zoukankan      html  css  js  c++  java
  • WPF MatrixTransform 的平滑动画代码

    注:此代码摘自:《Smooth animation using MatrixTransform?》

      由于MatrixAnimationUsingKeyFrames和DiscreteMatrixKeyFrame动画帧不会进行插值计算,因些,使用此方式只能产生生硬的帧切换效果。下面代码是通过自定义的LinearMatrixAnimation类,来达到平滑切换的效果。

    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    namespace MapControl
    {
        public class LinearMatrixAnimation : AnimationTimeline
        {
            public Matrix? From
            {
                set { SetValue(FromProperty, value);}
                get { return (Matrix)GetValue(FromProperty); }
            }
            public static DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(Matrix?), typeof(LinearMatrixAnimation), new PropertyMetadata(null));
            public Matrix? To
            {
                set { SetValue(ToProperty, value); }
                get { return (Matrix)GetValue(ToProperty); }
            }
            public static DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(Matrix?), typeof(LinearMatrixAnimation), new PropertyMetadata(null));
            public LinearMatrixAnimation()
            {            
            }
            public LinearMatrixAnimation(Matrix from, Matrix to, Duration duration)
            {
                Duration = duration;
                From = from;
                To = to;
            }
            public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock)
            {
                if (animationClock.CurrentProgress == null)
                {
                    return null;
                }
                double progress = animationClock.CurrentProgress.Value;
                Matrix from = From ?? (Matrix)defaultOriginValue;
                if (To.HasValue)
                {
                    Matrix to = To.Value;
                    Matrix newMatrix = new Matrix(((to.M11 - from.M11) * progress)+from.M11, 0, 0, ((to.M22 - from.M22) * progress)+from.M22,
                                                  ((to.OffsetX - from.OffsetX) * progress) + from.OffsetX, ((to.OffsetY - from.OffsetY) * progress)+ from.OffsetY);
                    return newMatrix;
                }
                return Matrix.Identity;
            }
            protected override System.Windows.Freezable CreateInstanceCore()
            {
                return new LinearMatrixAnimation();
            }
            public override System.Type  TargetPropertyType
            {
                get { return typeof(Matrix); }
            }
        }
    }

    转载的   可用

  • 相关阅读:
    【hdu6035】 Colorful Tree dfs序
    【GDOI2018模拟8】 数学竞赛 三角函数性质+记忆化搜索
    【BZOJ4184】shallot 线性基
    失分统计#1
    【learning】微信跳一跳辅助c++详解 轻松上万 【下】
    【2018北京集训十二】 coin 矩阵快速幂
    【learning】微信跳一跳辅助c++详解 轻松上万 【上】
    【2018北京集训6】Lcm DFT&FWT
    【BZOJ3143】【HNOI2013】游走 高斯消元
    【bzoj1855】 [Scoi2010]股票交易 单调队列优化DP
  • 原文地址:https://www.cnblogs.com/makubexsoft/p/2793915.html
Copyright © 2011-2022 走看看