拉伸动画ImageBrush到ImageBrush

时间:2018-08-19 13:12:16

标签: c# wpf

由于Koopakiller,我有了画笔动画。 它非常适合纯色到渐变色。 ImageBrush也可以使用,但仅适用于填充拉伸。我要穿制服。

我将这段代码用于笔刷

 BrushAnimation animation = new BrushAnimation
                {
                    From = CheckedBrush,
                    To = UncheckedBrush,
                    Duration = new Duration(TimeSpan.FromMilliseconds(300)),
                };

                Storyboard.SetTarget(animation, CBorder);
                Storyboard.SetTargetProperty(animation, new PropertyPath("Background"));

                var sb = new Storyboard();
                sb.Children.Add(animation);
                sb.Begin();

CheckedBrush和UncheckedBrush是Imagebrush。

BrushAnimation源代码:

 public class BrushAnimation : AnimationTimeline
{
    public override Type TargetPropertyType
    {
        get
        {
            return typeof(Brush);
        }
    }

    public override object GetCurrentValue(object defaultOriginValue,
                                           object defaultDestinationValue,
                                           AnimationClock animationClock)
    {
        return GetCurrentValue(defaultOriginValue as Brush,
                               defaultDestinationValue as Brush,
                               animationClock);
    }
    public object GetCurrentValue(Brush defaultOriginValue,
                                  Brush defaultDestinationValue,
                                  AnimationClock animationClock)
    {
        if (!animationClock.CurrentProgress.HasValue)
            return Brushes.Transparent;

        //use the standard values if From and To are not set 
        //(it is the value of the given property)
        defaultOriginValue = this.From ?? defaultOriginValue;
        defaultDestinationValue = this.To ?? defaultDestinationValue;

        if (animationClock.CurrentProgress.Value == 0)
            return defaultOriginValue;
        if (animationClock.CurrentProgress.Value == 1)
            return defaultDestinationValue;

        return new VisualBrush(new Border()
        {
            Width = 1,
            Height = 1,
            Background = defaultOriginValue,
            Child = new Border()
            {
                Background = defaultDestinationValue,
                Opacity = animationClock.CurrentProgress.Value,
            }
        });
    }

    protected override Freezable CreateInstanceCore()
    {
        return new BrushAnimation();
    }

    //we must define From and To, AnimationTimeline does not have this properties
    public Brush From
    {
        get { return (Brush)GetValue(FromProperty); }
        set { SetValue(FromProperty, value); }
    }
    public Brush To
    {
        get { return (Brush)GetValue(ToProperty); }
        set { SetValue(ToProperty, value); }
    }

    public static readonly DependencyProperty FromProperty =
        DependencyProperty.Register("From", typeof(Brush), typeof(BrushAnimation));
    public static readonly DependencyProperty ToProperty =
        DependencyProperty.Register("To", typeof(Brush), typeof(BrushAnimation));
}

CheckedBrush和UncheckedBrush具有均匀的拉伸度。但是动画只能填充动画,因此看起来很奇怪,因为动画后它变为统一的。 谢谢

0 个答案:

没有答案