刷到画笔动画

我设法找到了如何制作WPF动画 – 两种颜色之间的过渡。

它被称为ColorAnimation并且效果很好。

ColorAnimation animation = new ColorAnimation { From = Colors.DarkGreen, To = Colors.Transparent, Duration = new Duration(TimeSpan.FromSeconds(1.5)), AutoReverse = false }; animation.Completed += new EventHandler(animation_Completed); SolidColorBrush brush = new SolidColorBrush(Colors.Transparent); animation.AccelerationRatio = 0.5; Background = brush; brush.BeginAnimation(SolidColorBrush.ColorProperty, animation); 

我用它来动画我的usercontrol的背景。 我的控件背景是SolidColorBrush 。 最近我改为LinearGradientBrush 。 现在我不能再使用我的动画了。

我需要动画从画笔到画笔,而不是颜色到颜色。 最好的选择是抽象画笔类型,包括SolidColor,LinearGradient等,所以我可以动画,例如从SolidColorBrushLinearGradientBrush 。 这有可能吗? 谢谢。

另一种可能的方法是创建一个动画画笔的自定义动画类。 我通过创建一个派生自AnimationTimeline的类找到了一种简单的方法。 我们可以覆盖自定义类中的一些成员,其中包括AnimationTimeline.GetCurrentValue方法 。 它返回一个值,该值取决于动画进度以及开始和结束值。

最简单的方法是创建一个VisualBrush并使用子控件上的Opacity属性将end值与end值交叉淡化。 结果是如下所示的类:

 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)); } 

您可以像往常一样在XAML中使用它:

                

或者代码背后:

 var animation = new BrushAnimation { From = Brushes.Red, To = new LinearGradientBrush (Colors.Green, Colors.Yellow, 45), Duration = new Duration(TimeSpan.FromSeconds(5)), }; animation.Completed += new EventHandler(animation_Completed); Storyboard.SetTarget(animation, border); Storyboard.SetTargetProperty(animation, new PropertyPath("Background")); var sb = new Storyboard(); sb.Children.Add(animation); sb.Begin(); 

也可以使用构造函数重载等扩展BrushAnimation ,因此它看起来像.NET给定的动画类型。

您只需要在渐变画笔的渐变色块上使用颜色动画。 下面是一个使用故事板为矩形渐变设置动画的示例。

                             

如果您具有为填充画笔指定名称的模板样式,则可以为画笔的颜色设置动画,如下所示:

                

取自MSDN