刷来刷去动画 [英] Brush to Brush Animation
问题描述
我设法找到了如何让一个WPF动画 - 两种颜色之间的过渡
i managed to find out how to make a wpf animation - transition between two colors.
其称为ColorAnimation和行之有效的。
Its called ColorAnimation and works well.
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);
我使用的这个动画我的用户的背景。我的控制背景的SolidColorBrush。最近我改成一个LinearGradientBrush。现在我可以不再使用我的动画。
I am using this to animate background of my usercontrol. My controls background is solidcolorbrush. Recently i changed to LinearGradientBrush. Now i can use my animation no more.
我需要从动画刷来刷,而不是随颜色。而最好的办法是抽象刷型,其中包括的SolidColor,等的LinearGradient,这样我就可以制作动画,例如从到的SolidColorBrush刷的LinearGradient。是,即使可能吗?谢谢你。
I need animation from brush to brush, not color to color. And best option is Abstract brush type, which includes solidcolor, lineargradient etc, so i can animate for example from solidcolorbrush to lineargradient brush. Is that even possible? Thank you.
推荐答案
另一种可能的方式是,创建一个动画刷自定义animtion类。
我发现了一个简单的方法来做到这一点通过创建一个类,从<一个推导href=\"https://msdn.microsoft.com/en-us/library/system.windows.media.animation.animationtimeline.aspx\"><$c$c>AnimationTimeline$c$c>.我们可以覆盖一些成员在自定义类,除其他事项外的<一个href=\"https://msdn.microsoft.com/de-de/library/system.windows.media.animation.animationtimeline.getcurrentvalue.aspx\"><$c$c>AnimationTimeline.GetCurrentValue$c$c>方法。它返回一个值取决于动画进度和起点和最终值。
Another possible way is, to create a custom animtion class that animate brushes.
I found a simple way to do that by creating a class, derivated from AnimationTimeline
. We can override some members in the custom class, among other things the AnimationTimeline.GetCurrentValue
method. It returns a value depend on the animation progress and the start- and end value.
最简单的方法是创建一个的VisualBrush
并用透明度
属性最终值交叉衰减开始 - 对孩子的控制。其结果是像下面这样的类:
The simplest way is to create a VisualBrush
and crossfade the start- with the end value with the Opacity
property on a child control. The result is a class like the following:
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一如既往使用它:
You can use it as always in XAML:
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard >
<local:BrushAnimation Storyboard.TargetName="border"
Storyboard.TargetProperty="Background"
Duration="0:0:5" From="Red"
RepeatBehavior="Forever" AutoReverse="True" >
<local:BrushAnimation.To>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF00FF2E" Offset="0.005"/>
<GradientStop Color="#FFC5FF00" Offset="1"/>
<GradientStop Color="Blue" Offset="0.43"/>
</LinearGradientBrush>
</local:BrushAnimation.To>
</local:BrushAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
或code背后:
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给定动画类型。
It is also possible to extend the BrushAnimation
with constructor overloads etc., so it looks like a .NET given animation type.
这篇关于刷来刷去动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!