none
自定义动画类,如何加入缓动函数 RRS feed

  • 问题

  • 我在网上看到一个自定义的动画类,可以对GridLength这样的属性进行对话,我改了下,变成自己的DoubleAnimation

    但是不知道如何加入缓动函数,IEasingFunction 接口怎么用?

    internal class DoubleAni : AnimationTimeline
        {
            static DoubleAni()
            {
                FromProperty = DependencyProperty.Register("From", typeof(double),
                    typeof(DoubleAni));
    
                ToProperty = DependencyProperty.Register("To", typeof(double),
                    typeof(DoubleAni));
               
            }
    
            public override Type TargetPropertyType
            {
                get
                {
                    return typeof(double);
                }
            }
    
            protected override System.Windows.Freezable CreateInstanceCore()
            {
                return new DoubleAni();
            }
    
            public static readonly DependencyProperty FromProperty;
            public double From
            {
                get
                {
                    return (double)GetValue(DoubleAni.FromProperty);
                }
                set
                {
                    SetValue(DoubleAni.FromProperty, value);
                }
            }
    
            public static readonly DependencyProperty ToProperty;
            public double To
            {
                get
                {
                    return (double)GetValue(DoubleAni.ToProperty);
                }
                set
                {
                    SetValue(DoubleAni.ToProperty, value);
                }
            }
    
            public override object GetCurrentValue(object defaultOriginValue,
                object defaultDestinationValue, AnimationClock animationClock)
            {
                double fromVal = ((double)GetValue(DoubleAni.FromProperty));
                double toVal = ((double)GetValue(DoubleAni.ToProperty));
    
                if (fromVal > toVal)
                {
                    return (1 - animationClock.CurrentProgress.Value) * (fromVal - toVal) + toVal;
                }
                else
                    return animationClock.CurrentProgress.Value * (toVal - fromVal) + fromVal;
            }
        }
    

     


    Hero
    2011年10月27日 5:08

答案

  • 我还是老样子,反编译了.Net的代码,看了DoubleAnimation类,注意到,我们需要自己增加 EasingFunctionProperty 这个依赖属性:

    public static readonly DependencyProperty EasingFunctionProperty;

    然后定义它的属性签名:

     public IEasingFunction EasingFunction { get; set; }

    最后我们需要在 GetCurrentValue 方法中通过 EasingFunction 来修正Clock时间。

        double normalizedTime = animationClock.CurrentProgress.Value;
        IEasingFunction easingFunction = this.EasingFunction;
        if (easingFunction != null)
        {
            normalizedTime = easingFunction.Ease(normalizedTime);
        }
    
    

     

    Sincerely,

     


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 HeroHua0509 2011年10月28日 7:33
    2011年10月28日 5:54
    版主