原文:WPF实现左右移动(晃动)动画效果

实现控件或布局的左右移动(晃动)主要用到DoubleAnimation以及Storyboard

布局代码为:

<Canvas>
<Grid Width="200" Height="100" Background="MediumAquamarine" Name="GroupboxArea" Canvas.Left="100" Canvas.Top="200"/>
<Button Content="Button" Height="25" Width="78" Click="Button_Click"/>
</Canvas>

后台代码为:

 private void Button_Click(object sender, RoutedEventArgs e)
{
DoubleAnimation DAnimation = new DoubleAnimation();
DAnimation.From = 100;//起点
DAnimation.To = 280;//终点
DAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));//时间 Storyboard.SetTarget(DAnimation, GroupboxArea);
Storyboard.SetTargetProperty(DAnimation, new PropertyPath(Canvas.LeftProperty));
Storyboard story = new Storyboard(); story.Completed += new EventHandler(story_Completed);//完成后要做的事
//story.RepeatBehavior = RepeatBehavior.Forever;//无限次循环,需要的自己加上
story.Children.Add(DAnimation);
story.Begin();
}
void story_Completed(object sender, EventArgs e)
{
DoubleAnimation DAnimation = new DoubleAnimation();
DAnimation.From = 280;//起点
DAnimation.To = 100;//终点
DAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));//时间 Storyboard.SetTarget(DAnimation, GroupboxArea);
Storyboard.SetTargetProperty(DAnimation, new PropertyPath(Canvas.LeftProperty));
Storyboard story = new Storyboard(); story.Completed += new EventHandler(storyCompleted);//完成后要做的事
//story.RepeatBehavior = RepeatBehavior.Forever;//无限次循环,需要的自己加上
story.Children.Add(DAnimation);
story.Begin();
} void storyCompleted(object sender, EventArgs e)
{
DoubleAnimation DAnimation = new DoubleAnimation();
DAnimation.From = 100;//起点
DAnimation.To = 200;//终点
DAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));//时间 Storyboard.SetTarget(DAnimation, GroupboxArea);
Storyboard.SetTargetProperty(DAnimation, new PropertyPath(Canvas.LeftProperty));
Storyboard story = new Storyboard(); //story.Completed += new EventHandler(storyCompleted);//完成后要做的事
//story.RepeatBehavior = RepeatBehavior.Forever;//无限次循环,需要的自己加上
story.Children.Add(DAnimation);
story.Begin();
}

WPF实现左右移动(晃动)动画效果的更多相关文章

  1. WPF编游戏系列 之七 动画效果(2)

    原文:WPF编游戏系列 之七 动画效果(2)        上一篇已经对关闭窗口图标进行了动画效果处理,本篇将对窗口界面的显示和关闭效果进行处理.由于所有的动画效果都是针对窗口界面的Canvas,所以 ...

  2. WPF编游戏系列 之六 动画效果(1)

    原文:WPF编游戏系列 之六 动画效果(1)        本篇主要针对界面进行动画效果处理.首先在打开或关闭界面时,使其产生动态效果而不是生硬的显示或消失(如下图).其次在鼠标放到关闭窗口图标上时, ...

  3. WPF实现3D翻转的动画效果

    1.前端代码实现 1.1 原理见代码注析 <Grid MouseDown="Grid_MouseDown"> <Viewport3D> <Viewpo ...

  4. WPF 分享一种背景动画效果

    今天看微软的一个Samples,发现一个蛮好玩的背景样式,如下图所示: 风格比较卡哇伊. <Window x:Class="WPFSamplesTest.MainWindow" ...

  5. 动画(Animation) 之 (闪烁、左右摇摆、上下晃动等效果)

    左右晃动的效果: (这边显示没那么流畅) 一.续播  (不知道取什么名字好,就是先播放动画A, 接着播放动画B) 有两种方式. 第一种,分别动画两个动画,A和B, 然后先播放动画A,设置A 的 Ani ...

  6. wpf image控件循环显示图片 以达到动画效果 问题及解决方案

    1>最初方案: 用wpf的image控件循环显示图片,达到动画效果,其实就是在后台代码动态改变Image.Source的值,关键代码: ; i < ; i++)//六百张图片 { Bitm ...

  7. 【WPF】两则动画效果

    原文:[WPF]两则动画效果 引言 利用WPF的动画可以轻而易举的实现各种各样的特效,如擦除,滑动进入等,先看两个效果图 第一个效果 这个动画其实利用了OpacityMask和LinearGradie ...

  8. WPF 有趣的动画效果

    WPF 有趣的动画效果         这一次我要呈上一个简单的文章,关于给你的WPF apps加入美丽的光线动画,可是我对动画这东西可能有点入迷了.         实际上.我对动画如此的入迷,以至 ...

  9. WPF 画线动画效果实现

    原文:WPF 画线动画效果实现 弄了将近三天才搞定的,真是艰辛的实现. 看了很多博客,都太高深了,而且想要实现的功能都太强大了,结果基础部分一直实现不了,郁闷啊~ 千辛万苦终于找到了一个Demo,打开 ...

随机推荐

  1. C#实现人脸识别

    C#实现基于ffmpeg加虹软的人脸识别 关于人脸识别 目前的人脸识别已经相对成熟,有各种收费免费的商业方案和开源方案,其中OpenCV很早就支持了人脸识别,在我选择人脸识别开发库时,也横向对比了三种 ...

  2. 【Codeforces Round #439 (Div. 2) B】The Eternal Immortality

    [链接] 链接 [题意] 求b!/a!的最后一位数字 [题解] b-a>=20的话 a+1..b之间肯定有因子2和因子5 答案一定是0 否则暴力就好 [错的次数] 在这里输入错的次数 [反思] ...

  3. [Vue] Build Vue.js Apps with the Vue-CLI and Nuxt.js

    The vue-cli allows you to easily start up Vue projects from the command line while Nuxt.js enables a ...

  4. HTTP协议和HTTPS协议初探

    概况 HTTP是hypertext transfer protocol(超文本传输协议)的简写.它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEBserver之间交换数据的过程. HT ...

  5. mycat server.xml 配置文件详解

    <?xml version="1.0" encoding="UTF-8"?> <!-- - - Licensed under the Apac ...

  6. [Angular Unit Testing] Testing Component methods

    import {ComponentFixture, TestBed} from '@angular/core/testing'; import {BrowserDynamicTestingModule ...

  7. Django之富文本编辑器kindeditor 及上传

    1.什么是富文本编辑器 百度百科(https://baike.baidu.com/item/%E5%AF%8C%E6%96%87%E6%9C%AC%E7%BC%96%E8%BE%91%E5%99%A8 ...

  8. 【b803】传纸条

    Time Limit: 1 second Memory Limit: 50 MB [问题描述] 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行 ...

  9. 【codeforces 758C】Unfair Poll

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  10. 【u226】查单词

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 全国英语四级考试就这样如期到来了.可是小Y依然没有做好充分的准备.为了能够大学毕业,可怜的小Y决定作弊 ...