码迷,mamicode.com
首页 > Windows程序 > 详细

WPF后台动画DoubleAnimation讲解

时间:2018-04-28 21:05:23      阅读:386      评论:0      收藏:0      [点我收藏+]

标签:函数   public   播放   缩放动画   nim   移动元素   移动动画   目标   complete   

WPF后台动画,使用DoubleAnimation做的。

1.移动动画

需要参数(目标点离最上边的位置,目标点离最左边的位置,元素名称)

Image mImage = new Image();

FloatInElement(100,100,mImage);

 1 /// <summary>
 2         /// 移动动画 
 3         /// </summary>
 4         /// <param name="top">目标点相对于上端的位置</param>
 5         /// <param name="left">目标点相对于左端的位置</param>
 6         /// <param name="elem">移动元素</param>
 7         public static void FloatInElement(double top, double left, UIElement elem)
 8         {
 9             try
10             {
11                 DoubleAnimation floatY = new DoubleAnimation()
12                 {
13                     To = TransValueFromHeight(top),
14                     Duration = new TimeSpan(0, 0, 0, 1, 0),
15                 };
16                 DoubleAnimation floatX = new DoubleAnimation()
17                 {
18                     To = TransValueFromWidth(left),
19                     Duration = new TimeSpan(0, 0, 0, 1, 0),
20                 };
21                 
22                 elem.BeginAnimation(Canvas.TopProperty, floatY);
23                 elem.BeginAnimation(Canvas.LeftProperty, floatX);
24             }
25             catch (Exception)
26             {
27 
28                 throw;
29             }
30         }

2.透明度动画

需要参数(元素名称,需要到达的透明度)

Image mImage = new Image();

FloatInElement(mImage,0);

 1 /// <summary>
 2         /// 透明度动画 
 3         /// </summary>
 4         /// <param name="elem"></param>
 5         /// <param name="to"></param>
 6         public static void FloatElement(UIElement elem, double to)
 7         {
 8             lock (elem)
 9             {
10                 if (to == 1)
11                 {
12                     elem.Visibility = Visibility.Visible;
13                 }
14                 DoubleAnimation opacity = new DoubleAnimation()
15                 {
16                     To = to,
17                     Duration = new TimeSpan(0, 0, 0, 1, 0)
18                 };
19                 EventHandler handler = null;
20                 opacity.Completed += handler = (s, e) =>
21                 {
22                     opacity.Completed -= handler;
23                     if (to == 0)
24                     {
25                         elem.Visibility = Visibility.Collapsed;
26                     }
27                     opacity = null;
28                 };
29                 elem.BeginAnimation(UIElement.OpacityProperty, opacity);
30             }
31         }

3.缓动动画-缩放动画

需要参数(控件名称,元素开始的位置,开始大小,目标大小)

Image mImage = new Image();

ScaleEasingAnimationShow(mImage,new Point(0.5,0.5),1,0);

元素开始的位置是从new Point(0,0)到new Point(1,1),左上角为(0,0)、右下角为(1,1),控件大小范围为(1,0) 1为控件本身大小,0 为控件缩放完,不显示。

如果要改动画类型,则改EasingMode = EasingMode.EaseOut

 1 /// <summary>
 2         /// 用户控件是的动画
 3         /// </summary>
 4         /// <param name="element">控件名</param>
 5         /// <param name="point">元素开始动画的位置</param>
 6         /// <param name="from">元素开始的大小</param>
 7         /// <param name="from">元素到达的大小</param>
 8         public static void ScaleEasingAnimationShow(FrameworkElement element, Point point, double from, double to)
 9         {
10             lock (element)
11             {
12                 ScaleTransform scale = new ScaleTransform();
13                 element.RenderTransform = scale;
14                 element.RenderTransformOrigin = point;//定义圆心位置        
15                 EasingFunctionBase easeFunction = new PowerEase()
16                 {
17                     EasingMode = EasingMode.EaseOut,
18                     Power = 5
19                 };
20                 DoubleAnimation scaleAnimation = new DoubleAnimation()
21                 {
22                     From = from,                                   //起始值
23                     To = to,                                     //结束值
24                     EasingFunction = easeFunction,                    //缓动函数
25                     Duration = new TimeSpan(0, 0, 0, 1, 0)  //动画播放时间
26                 };
27                 AnimationClock clock = scaleAnimation.CreateClock();
28                 scale.ApplyAnimationClock(ScaleTransform.ScaleXProperty, clock);
29                 scale.ApplyAnimationClock(ScaleTransform.ScaleYProperty, clock);
30             }
31         }

 

WPF后台动画DoubleAnimation讲解

标签:函数   public   播放   缩放动画   nim   移动元素   移动动画   目标   complete   

原文地址:https://www.cnblogs.com/softwyy/p/8969184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!