原文: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. Win7长时间使用占用内存高问题记

    工作电脑Win7 64位,8G内存,没设置虚拟内存,连续运行几天,中间只是睡眠,今天在试用时总提示内存不足,看任务管理器已经把占用内存比较多的几个进程都结束掉了,但内存占用依旧是80%以上,eclip ...

  2. IDC报告:从IaaS到PaaS,阿里云主导云计算市场

    11月7日,著名国际研究机构IDC发布了2017年上半年中国公共云PaaS市场调研结果,阿里云以27%的份额保持压倒性领先,是第二名的近三倍.结合IDC上一份IaaS市场的报告(阿里云第一,占比47. ...

  3. 【2006】求N!的精确值

    Time Limit: 3 second Memory Limit: 2 MB 对于阶乘函数,即使自变量较小,其函数值也会相当大.例如: 10!=3628800 25!=155112100433309 ...

  4. Boost.Asio c++ 网络编程翻译(10)

    read/write方法 这些方法对一个流进行读写操作(能够是套接字,或者其它表现的像流的类): async_read(stream, buffer [, completion],handler):这 ...

  5. yield return

    一次被yield return坑的历程.   事情的经过是这样的: 我用C#写了一个很简单的一个通过迭代生成序列的函数. public static IEnumerable<T> Iter ...

  6. html5 背景音乐 js控制播放 暂停

    <html> <head> <title> 测试页面 </title> <script src="jquery.min.js" ...

  7. 【54.38%】【BZOJ 4300】绝世好题

    Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 1120  Solved: 609 [Submit][Status][Discuss] Descript ...

  8. eclipse jdt

    http://www.cnblogs.com/hoojo/p/use_eclipse_ant_javac_JDT_compiler_class.html

  9. Mysql存储过程中使用cursor

    一.表 学生表 CREATE TABLE `t_student` (    `stuNum` int(11) NOT NULL auto_increment,    `stuName` varchar ...

  10. Qt Roadmap for 2018(对3D有很多改进)

    When it comes to new features, we have many things ongoing related to graphics, so I’ll start with t ...