Based on C # WPF framework - Animation

WPF provides a more advanced model, we can only focus on the definition animation of this model, regardless of their rendering. This model is based on a dependency property infrastructure. Essentially, WPF animation is just repair dyed way for some time interval. This model is based on a dependency property infrastructure. Essentially, WPF animation simply changing the way dependency property value within a time interval.

Although WPF three methods may be used (as an animated linear interpolation, and the path key frames ), but also create more complete use completely different ways to modify the value of the animation classes. The only requirement is that the custom animation class must be based on modify the time value.
Many completely different ways to modify the value of the animation classes. The only requirement is that the custom animation class must be modified according to the time value.

Animation category

All animation classes are " type name + Animation named" approach. This view is very close to the actual situation, but not very accurate.

  • 17 "type name + Animation" classes using interpolation == ==.
  • 22 "type name + AnimationUsingKeyFrames" classes that use == == keyframe animation.
  • 3 "type name + AnimationUsingPath" classes using == == based animation path.
    All these classes inherit from abstract animation of "type name + AnimationBase" category, these base classes implement some basic functions, providing shortcuts for creating custom animation class. If a data type to support multiple types of animation, all animation classes inherit from abstract base class animation.

    This 42 classes are not the only namespace System.Windows.Media.Animation - - content. Each keyframe animation keyframes also use your own classes and keyframes collections, these two classification can lead to some confusion. In short, there are more than 100 classes == System.Windows.Media.Animation == namespace.

BooleanAnimationUsingKeyFrames ByteAnimation
ByteAnimationUsingKeyFrames CharAnimationUsingKeyFrames
ColorAnimation ColorAnimationUsingKeyFrames
DecimalAnimation DecimalAnimationUsingKeyFrames
DoubleAnimation DoubleAnimationUsingKeyFrames
DoubleAnimationUsingPath Int16Animation
Intl 6AnimationUsingKeyFrames Int32Animation
Int32AnimationUsingKeyFrames Int64Animation
Int64AnimationUsingKeyFrames MatrixAnimationUsingKeyFrames
MatrixAnimationUsingPath ObjectAnimationUsingKeyFrames .
Fointanimtion PointAnimationUsingKeyFrames
Fointanimtionusidagfth Point3DAnimation
Point3DAnimationUsingKeyFrames QuarternionAnimation
QuarternionAnimationUsingKeyFrames RectAnimation
RectAnimationUsingKeyFrames Rotation3DAnimation
Rotation3DAnimationUsingKeyFrames SingleAnimation
SingleAnimationUsingKeyFrames SizeAnimation
SizeAnimationUsingKeyFrames StringAnimationUsingKeyFrames
ThicknessAnimation ThicknessAnimationUsingKeyFrames
VectorAnimation VectorAnimationUsingKeyFrames
Vector3DAnimation Vector3DAnimationUsingKeyFrames

This is not the 42 open class System. Windows.Media.Animation unique within the namespace of the valley. Each keyframe animation
also use your own key frames and keyframes class collections, these two classification can lead to some confusion. In short, in System.Windows
Media.Animation namespace more than 100 categories.

Any use of linear interpolation animation requires a minimum of three facts :
== start value (From) ==, == end value (To) time (the Duration) and == == == executed entire animation.
To property without using, By properties may be used. By attribute is used to create animation by changing the value of the number, rather than by changing the value of a given target.
By using the From property and can be combined, but this does not reduce any work. By value is simply added to the From value, to reach a value To.
Most interpolation animation classes often provide By attributes, but not all the case. For example, for non-numeric data types,, By property it is meaningless, such as classes used ColorAnimation Color structure.
A method otherwise similar behavior can be obtained without the use of a property can create animated By increasing the value by setting IsAdditive property. When creating this animation, the current value is automatically added to the From value and To value.

Animation of the life cycle

One-way animation (such as animations growth button) at the end of the run will remain active, because the animation is necessary to keep the width of the button for the new value. This leads to the following is not a common problem if you try to use code to modify a property value after the animation is complete, the code will not work. Because the code only specifies a new local value for the property, but the property value after the animation will still take priority.
According ready to complete the work by several methods to solve this problem as follows:
● create the element is reset to its original state of animation. The purpose can be achieved by creating not set To attribute animation.
For example, the width of the button is reduced to the size of the last set button animation is reduced, then you can use this property of the code changes.
● create flip animation. To create flip animation by AutoReverse property is set to true. For example, when the button growth - no increase in the width of animated buttons, reverse play the animation, returned to the original width. The total duration of the animation will be doubled.
● change FillBehavior property. Usually, FillBehavior property is set to HoldEnd, which means that when the end of the animation, will continue to apply the final value of the target element. If FillBehavior property to Stop, as long as the end of the animation, the property will revert to the original value.
● When the animation is completed by deleting the animation object animation Completed event handler object.
Top 3 Ways To change the behavior of the animation. Either way, they will attribute the animation to its original value. If this is not desired, it would need to use the last - ways.
First, before starting the animation, the associated event handlers in response to completion of the animation events:
widthAnimation the Completed + = animation_ the Completed;.
== Note: ==
the Completed event is a conventional event .NET, without using a conventional EventArgs objects additional information. The event is not routed event.

Timeline class
name Explanation
start Time The setting will be added to the delay time (TimeSpan type) before the movie starts. This - is added to the total time delay, so the animation having 5 sec 5 sec delay, the total time is 10 seconds. When the synchronization start at the same time, but different animation application effect of the order, BeginTime property is useful
Duration Use Duration animate objects from the beginning to the end of the running time
Speed ​​Ratio Increase speed or slow down animation. Typically, SpeedRatio attribute value is 1. If the property value increases, accelerating the animation (e.g., if the attribute value SpeedRatio 5, the animation speed becomes 5 times the original); if the attribute value is reduced, the animation will slow down (e.g., if the attribute SpeedRatio value of 0.5, the animation time becomes twice the original). To be worth the same results by changing the animation Duration property. When applied BeginTime delay, irrespective of the value of the attribute SpeedRatio
AccclerationRatio;DecelerationRatio So that the animation is not linear, and is slow to start, and the growth rate (by increasing AcelerationRatio attribute values): the speed is reduced or ended (by increasing DecelerationRatio attribute values). These two attributes are values ​​between 0 and 1, and are set to 0 at the beginning. In addition, the sum of these two properties can not be more than 1
AutoReverse If true, the animation is completed when the player will automatically reverse and return to its original value. It will also make the animation run time is doubled. If property values ​​increase SpeedRatio, it will be applied to the original animation and reverse the animation. BeginTime property value is applied to only the animation does not delay the beginning of a reverse animation
FillBehavior Decide what to do when the move is completed. Typically, the property value can be kept constant end value (FillBchavior. HoldEnd), but can also choose to return to the original value of the attribute number (FillBehavior Stop)
RepeatBehavior By this property, you can be used to specify the number of intervals or repeating animation. RepeatBehavior object is used to set this attribute determines the exact behavior
Storyboard

在所有声明式动画中都会用到如下两个要素:
故事板: 故事板是BeginAnimation( )方法的XAML等价物。通过故事板将动画指定到合适的元素和属性。
事件触发器: 事件触发器响应属性变化或事件(如按钮的Click事件),并控制故事板。例如,为了开始动画,事件触发器必须开始故事板。
小案例:
在这里插入图片描述

以上小案例为点单击按钮时,原形先向右移动到达To值后继续向左移动,矩形向右下角移动,当到达To值后原路返回向左上角移动,此动画为重复执行动画

首先需要引入动画类命名空间;

using System.Windows.Media.Animation;

创建故事板:

        Storyboard sto = new Storyboard(); // 故事板
        Storyboard stt = new Storyboard(); // 向下故事板

初始化图形大小,位置:

            Border bod = new Border(); // 圆形
            bod.Width = 100;
            bod.Height = 100;
            bod.BorderThickness =new Thickness(10); // 设置原形边框厚度
            bod.BorderBrush = Brushes.Yellow; // 边框颜色
            bod.Background = Brushes.Purple; // 圆形背景色
            bod.CornerRadius = new CornerRadius(50); // 画圆
            BG.Children.Add(bod);
            Border boo = new Border(); // 矩形
            boo.Width = 100;
            boo.Height = 100;
            boo.Background = Brushes.Cyan;
            Canvas.SetLeft(boo, 100);
            Canvas.SetTop(boo, 300);
            boo.CornerRadius = new CornerRadius(20);
            BG.Children.Add(boo);

动画情节:

DoubleAnimation move = new DoubleAnimation(); // 移动情节
            move.From = 0; // 初始值
            move.To = 400; // 结束值
            move.Duration = new Duration(new TimeSpan(0, 0, 0, 3, 0)); // 需要的时间
            move.AutoReverse = true; // 设置成返回
            move.RepeatBehavior = RepeatBehavior.Forever; // 重复执行
            Storyboard.SetTarget(move,bod); // 情节添加给对象
            //Storyboard.SetTargetProperty(move, new PropertyPath(Canvas.LeftProperty)); // 改变lef位置
            Storyboard.SetTargetProperty(move, new PropertyPath("(Canvas.Left)"));
            sto.Children.Add(move);

            ColorAnimation bianSe = new ColorAnimation(Colors.Purple,Colors.Orange,TimeSpan.FromMilliseconds(300)); // 改背景色情节
            Storyboard.SetTarget(bianSe, bod);
            Storyboard.SetTargetProperty(bianSe, new PropertyPath("(Border.Background).(SolidColorBrush.Color)"));
            sto.Children.Add(bianSe);

            ColorAnimation biankuSe = new ColorAnimation(Colors.Blue, Colors.DarkViolet,TimeSpan.FromSeconds(3));  // 改变边框颜色
            Storyboard.SetTarget(biankuSe, bod);
            Storyboard.SetTargetProperty(biankuSe, new PropertyPath("(Border.BorderBrush).(SolidColorBrush.Color)"));
            sto.Children.Add(biankuSe);

            ThicknessAnimation kuang=new ThicknessAnimation(new Thickness(10),new Thickness(20),TimeSpan.FromMilliseconds(30));  // 改变边框厚度
            Storyboard.SetTarget(kuang, bod);
            Storyboard.SetTargetProperty(kuang, new PropertyPath("BorderThickness"));
            sto.Children.Add(kuang);

            DoubleAnimation zhuan = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(3))); // 旋转情节
            Storyboard.SetTarget(zhuan, bod);
            zhuan.AutoReverse = true;
            zhuan.RepeatBehavior = RepeatBehavior.Forever;
            Storyboard.SetTargetProperty(zhuan, new PropertyPath("RenderTransform.Angle"));
            sto.Children.Add(zhuan);
            RotateTransform xuanZhuan = new RotateTransform(); // 旋转对象
            bod.RenderTransform = xuanZhuan;
            bod.RenderTransformOrigin = new Point(0.5,0.5);

            // 控制boo  向下移动并 旋转
            DoubleAnimation yidong = new DoubleAnimation();// 移动右
            yidong.From = 0;
            yidong.To = 500;
            yidong.Duration = new Duration(TimeSpan.FromSeconds(3)); // 设置毫秒数
            yidong.AutoReverse = true; // 返回原路
            yidong.RepeatBehavior = RepeatBehavior.Forever; // 重复执行
            Storyboard.SetTarget(yidong, boo);
            Storyboard.SetTargetProperty(yidong, new PropertyPath("(Canvas.Left)"));
            stt.Children.Add(yidong);
            DoubleAnimation yidong_ = new DoubleAnimation(Canvas.GetTop(boo),(Canvas.GetTop(boo)+400),new Duration(TimeSpan.FromSeconds(3))); // 移动下
            yidong_.AutoReverse = true;
            yidong_.RepeatBehavior = RepeatBehavior.Forever;
            Storyboard.SetTarget(yidong_, boo);
            Storyboard.SetTargetProperty(yidong_,new PropertyPath("(Canvas.Top)"));
            stt.Children.Add(yidong_);
            DoubleAnimation xiazhuan = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(3))); //旋转情节
            Storyboard.SetTarget(xiazhuan, boo);
            xiazhuan.AutoReverse = true; //可原路返回
            xiazhuan.RepeatBehavior = RepeatBehavior.Forever;// 重复执行
            Storyboard.SetTargetProperty(xiazhuan, new PropertyPath("RenderTransform.Angle"));
            stt.Children.Add(xiazhuan);
            RotateTransform xuanzhuan = new RotateTransform(); // 旋转对象
            boo.RenderTransform = xuanzhuan;
            boo.RenderTransformOrigin = new Point(0.5, 0.5);

点击按钮:

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            sto.Begin(); // 开启动画
            stt.Begin();
        }

Guess you like

Origin www.cnblogs.com/dust-856/p/12080006.html