• WPF动画的使用


    前言

    弹幕是什么?这里是使用动画将控件弹起来,通过C#提供的多样化动画类型,我们可以制做出丰富的界面效果。主要有基于时间的动画和基于属性的动画。

    1、Animatable

    一个提供动画支持的抽象类

    继承

    Object

    DispatcherObject

    DependencyObject

    Freezable

    Animatable

    2、DoubleAnimation 类

    在指定的 Duration 期间使用线性内插对两个目标值之间的 Double 属性值进行动画处理。是一种简单线性动画,仅由变化起点、变化终点、变化幅度、变化时间4个要素构成。三个细节:开始值(From)、结束值(To)和整个动画执行的时间(Duration)。

    1. private void Button_Click(object sender, RoutedEventArgs e)
    2. {
    3. DoubleAnimation daX = new DoubleAnimation();
    4. DoubleAnimation daY = new DoubleAnimation();
    5. //设置反弹
    6. BounceEase be = new BounceEase();
    7. be.Bounces = 3;
    8. be.Bounciness = 3;
    9. daY.EasingFunction = be;
    10. //指定起点
    11. daX.From = 300D;
    12. daY.From = 300D;
    13. //指定终点
    14. //Random r = new Random();
    15. //daX.To = r.NextDouble() * 300;
    16. //daY.To = r.NextDouble() * 300;
    17. //指定时长
    18. Duration duration = new Duration(TimeSpan.FromMilliseconds(2000));
    19. daX.Duration = duration;
    20. daY.Duration = duration;
    21. //动画的主体是TranslateTransform变形,而非Button
    22. this.tt.BeginAnimation(TranslateTransform.XProperty, daX);
    23. this.tt.BeginAnimation(TranslateTransform.YProperty, daY);
    24. }

    3、DoubleAnimationUsingPath

    使用 PathGeometry 在两个或多个目标值之间对 Double 属性值进行动画处理,以指定这些值。 此动画可用于沿路径移动可视对象。

    1. //从XAML代码中获取移动路径数据
    2. PathGeometry pg = this.LayoutRoot.FindResource("movingPath") as PathGeometry;
    3. Duration duration = new Duration(TimeSpan.FromMilliseconds(600));
    4. //创建动画
    5. DoubleAnimationUsingPath dapX = new DoubleAnimationUsingPath();
    6. dapX.PathGeometry = pg;
    7. dapX.Source = PathAnimationSource.X;
    8. dapX.Duration = duration;
    9. DoubleAnimationUsingPath dapY = new DoubleAnimationUsingPath();
    10. dapY.PathGeometry = pg;
    11. dapY.Source = PathAnimationSource.Y;
    12. dapY.Duration = duration;
    13. //自动返回、永远循环
    14. dapX.AutoReverse = true;
    15. dapX.RepeatBehavior = RepeatBehavior.Forever;
    16. dapY.AutoReverse = true;
    17. dapY.RepeatBehavior = RepeatBehavior.Forever;
    18. //执行动画
    19. this.tt.BeginAnimation(TranslateTransform.XProperty, dapX);
    20. this.tt.BeginAnimation(TranslateTransform.YProperty, dapY);

    4、DoubleAnimationUsingKeyFrames

    根据一组 KeyFrames 对 Double 属性的值进行动画处理。

    关键帧动画的目标值由其 KeyFrames 属性定义,该属性包含对象的集合 DoubleKeyFrame 。 每个都 DoubleKeyFrame 定义了动画的一段,其自己的目标和 ValueKeyTime。 当动画运行时,它会在指定的关键时间从一个键值前进到下一个键值。

    有三种类型的 DoubleKeyFrame 类,每个支持的内插方法各有一种: LinearDoubleKeyFrame、 DiscreteDoubleKeyFrame和 SplineDoubleKeyFrame。

    与 不同, DoubleAnimation可以 DoubleAnimationUsingKeyFrames 具有两个以上的目标值。 还可以控制单个 DoubleKeyFrame 段的内插方法。

    1. DoubleAnimationUsingKeyFrames dakX = new DoubleAnimationUsingKeyFrames();
    2. DoubleAnimationUsingKeyFrames dakY = new DoubleAnimationUsingKeyFrames();
    3. //设置动画总时长
    4. dakX.Duration = new Duration(TimeSpan.FromMilliseconds(900));
    5. dakY.Duration = new Duration(TimeSpan.FromMilliseconds(900));
    6. //创建、添加关键帧
    7. LinearDoubleKeyFrame x_kf_1 = new LinearDoubleKeyFrame();
    8. LinearDoubleKeyFrame x_kf_2 = new LinearDoubleKeyFrame();
    9. LinearDoubleKeyFrame x_kf_3 = new LinearDoubleKeyFrame();
    10. x_kf_1.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(300));
    11. x_kf_1.Value = 200;
    12. x_kf_2.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(600));
    13. x_kf_2.Value = 0;
    14. x_kf_3.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(900));
    15. x_kf_3.Value = 200;
    16. dakX.KeyFrames.Add(x_kf_1);
    17. dakX.KeyFrames.Add(x_kf_2);
    18. dakX.KeyFrames.Add(x_kf_3);
    19. LinearDoubleKeyFrame y_kf_1 = new LinearDoubleKeyFrame();
    20. LinearDoubleKeyFrame y_kf_2 = new LinearDoubleKeyFrame();
    21. LinearDoubleKeyFrame y_kf_3 = new LinearDoubleKeyFrame();
    22. y_kf_1.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(300));
    23. y_kf_1.Value = 0;
    24. y_kf_2.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(600));
    25. y_kf_2.Value = 180;
    26. y_kf_3.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(900));
    27. y_kf_3.Value = 180;
    28. dakY.KeyFrames.Add(y_kf_1);
    29. dakY.KeyFrames.Add(y_kf_2);
    30. dakY.KeyFrames.Add(y_kf_3);
    31. //执行动画
    32. this.tt.BeginAnimation(TranslateTransform.XProperty, dakX);
    33. this.tt.BeginAnimation(TranslateTransform.YProperty, dakY);

    5、高级动画

    AccelerationRation加速速率,介于0.0和0.1之间。

    DecelerationRation减速速度,介于0.0和0.1之间。

    SpeedRation动画实际播放速度与正常速度的比值。

    AutoReverse是否以相反的动画方式从终止值返回起始值。

    RepeatBehavior动画重复行为,取0为不播放,使用double类型值 可控制循环次数,取RepeatBehavior.Forver为永远循环。

    EasingFunction缓冲式渐变。

    6、场景动画

    并行执行的一组动画。

    以交互方式控制情节提要,可控制的情节提要可以暂停、恢复、查找、停止和删除。
    数据绑定和对时间线进行动画处理。

    后台代码方式实现

    1. Duration duration = new Duration(TimeSpan.FromMilliseconds(600));
    2. //红色小球匀速移动
    3. DoubleAnimation daRx = new DoubleAnimation();
    4. daRx.Duration = duration;
    5. daRx.To = 600;
    6. //绿色小球变速运动
    7. DoubleAnimationUsingKeyFrames dakGx = new DoubleAnimationUsingKeyFrames();
    8. dakGx.Duration = duration;
    9. SplineDoubleKeyFrame kfG = new SplineDoubleKeyFrame(600, KeyTime.FromPercent(1.0));
    10. kfG.KeySpline = new KeySpline(1, 0, 0, 1);
    11. dakGx.KeyFrames.Add(kfG);
    12. //蓝色小球变速运行
    13. DoubleAnimationUsingKeyFrames dakBx = new DoubleAnimationUsingKeyFrames();
    14. dakBx.Duration = duration;
    15. SplineDoubleKeyFrame kfB = new SplineDoubleKeyFrame(600, KeyTime.FromPercent(1.0));
    16. kfB.KeySpline = new KeySpline(0, 1, 1, 0);
    17. dakBx.KeyFrames.Add(kfB);
    18. //创建场景
    19. Storyboard storyboard = new Storyboard();
    20. Storyboard.SetTargetName(daRx, "ttR");
    21. Storyboard.SetTargetProperty(daRx, new PropertyPath(TranslateTransform.XProperty));
    22. Storyboard.SetTargetName(dakGx, "ttG");
    23. Storyboard.SetTargetProperty(dakGx, new PropertyPath(TranslateTransform.XProperty));
    24. Storyboard.SetTargetName(dakBx, "ttB");
    25. Storyboard.SetTargetProperty(dakBx, new PropertyPath(TranslateTransform.XProperty));
    26. storyboard.Duration = duration;
    27. storyboard.Children.Add(daRx);
    28. storyboard.Children.Add(dakGx);
    29. storyboard.Children.Add(dakBx);
    30. storyboard.Begin(this);
    31. storyboard.Completed += (a, b) => { MessageBox.Show(ttR.X.ToString()); };

    前台XAML方式实现

    1. <Button Content="Go!" Grid.Column="1" Grid.RowSpan="3">
    2. <Button.Triggers>
    3. <EventTrigger RoutedEvent="Button.Click">
    4. <BeginStoryboard>
    5. <Storyboard Duration="0:0:0:6">
    6. <DoubleAnimation Storyboard.TargetProperty="X"
    7. Storyboard.TargetName="ttR"
    8. To="400"
    9. Duration="0:0:0:6" />
    10. <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="X"
    11. Storyboard.TargetName="ttG"
    12. Duration="0:0:0:6" >
    13. <SplineDoubleKeyFrame KeyTime="0:0:0:6" Value="400" KeySpline="1,0,0,1"/>
    14. DoubleAnimationUsingKeyFrames>
    15. <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="X"
    16. Storyboard.TargetName="ttB"
    17. Duration="0:0:0:6" >
    18. <SplineDoubleKeyFrame KeyTime="0:0:0:6" Value="400" KeySpline="0,1,1,0"/>
    19. DoubleAnimationUsingKeyFrames>
    20. Storyboard>
    21. BeginStoryboard>
    22. EventTrigger>
    23. Button.Triggers>
    24. Button>

  • 相关阅读:
    力扣刷题-移除指定值的链表元素
    【jvm优化超详细】常见的JVM调优场景
    经典面试题-小于N的最大数
    android 12.0app应用卸载黑名单
    2023年CSP-J1入门级第一轮题解
    Vue3——网站整体布局、用户动态页面(下)
    To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
    【JAVA学习笔记】46 - (43)第十一章作业
    SurroundDepth拜读:自监督环视多相机深度估计
    docker资源控制
  • 原文地址:https://blog.csdn.net/weixin_45114627/article/details/141029011