gpt4 book ai didi

wpf - 使用不同的画笔动画颜色属性

转载 作者:行者123 更新时间:2023-12-04 00:40:19 25 4
gpt4 key购买 nike

我在 ControlTemplate.Resources 中有以下内容:

<ColorAnimation
Storyboard.TargetName="border"
Storyboard.TargetProperty="Background.(SolidColorBrush.Color)"
To="Orange"
Duration="0:0:0.2" />

如果我想更改为橙色的原始背景是纯色,则一切正常。但是当原始背景是 LinearGradientBrush 时,我也希望有这项工作。在第二种情况下,动画尝试更改属性是徒劳的,没有任何反应。

如何指定替换背景的动画,而不管它之前是什么类型?

最佳答案

如果您的背景LinearGradientBrush,那么您必须将每个GradientStop设置为您想要的Color即在这种情况下为橙色:

      <Storyboard x:Key="Storyboard1">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="Background.(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border">
<EasingColorKeyFrame KeyTime="0:0:2" Value="Orange"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="Background.(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border">
<EasingColorKeyFrame KeyTime="0:0:2" Value="Orange"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>

但是如果你想Animate整个Brush而不考虑它的类型那么你将不得不创建你自己的Animation。我创建了自己的 BrushAnimation 类来为画笔设置动画

    public class BrushAnimation : AnimationTimeline
{
static BrushAnimation()
{
FromProperty = DependencyProperty.Register("From", typeof(Brush),
typeof(BrushAnimation),new PropertyMetadata(new SolidColorBrush()));

ToProperty = DependencyProperty.Register("To", typeof(Brush),
typeof(BrushAnimation), new PropertyMetadata(new SolidColorBrush()));
}

public override Type TargetPropertyType
{
get
{
return typeof(Brush);
}
}

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

public static readonly DependencyProperty FromProperty;
public Brush From
{
get
{
return (Brush)GetValue(BrushAnimation.FromProperty);
}
set
{
SetValue(BrushAnimation.FromProperty, value);
}
}

public static readonly DependencyProperty ToProperty;
public Brush To
{
get
{
return (Brush)GetValue(BrushAnimation.ToProperty);
}
set
{
SetValue(BrushAnimation.ToProperty, value);
}
}


public override object GetCurrentValue(object defaultOriginValue,
object defaultDestinationValue, AnimationClock animationClock)
{
Brush fromVal = ((Brush)GetValue(BrushAnimation.FromProperty));
Brush toVal = ((Brush)GetValue(BrushAnimation.ToProperty));

SolidColorBrush solid = toVal as SolidColorBrush;

if(fromVal is LinearGradientBrush)
{
LinearGradientBrush brush = fromVal as LinearGradientBrush;
LinearGradientBrush newBrush = new LinearGradientBrush();
foreach(var stop in brush.GradientStops)
{
ColorAnimation animation = new ColorAnimation(stop.Color,solid.Color,this.Duration);
Color color = animation.GetCurrentValue(stop.Color, solid.Color, animationClock);
newBrush.GradientStops.Add(new GradientStop(color,stop.Offset));
}

return newBrush;
}
else
{
SolidColorBrush brush = fromVal as SolidColorBrush;
SolidColorBrush newsolid = new SolidColorBrush();
ColorAnimation solidAnimation = new ColorAnimation(brush.Color, solid.Color, this.Duration);
newsolid.Color = solidAnimation.GetCurrentValue(brush.Color, solid.Color, animationClock);

return newsolid;

}

}

我正在使用这个动画来为我的窗口上的 Canvas.Background 制作动画

    <Storyboard x:Key="MyStoryBoard" RepeatBehavior="Forever" AutoReverse="True">
<local:BrushAnimation Storyboard.TargetName="Canvas1"
Storyboard.TargetProperty = "(Canvas.Background)"
To="Orange" Duration="0:0:5"/>
</Storyboard>

并且您可以使用 StaticResource 设置动画的 From 属性,或者在您的代码隐藏中将其设置为控件的背景,例如:

    public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
((BrushAnimation) ((Storyboard) Resources["SolidStoryBoard"]).Children[0]).From = Canvas1.Background;
}
}

关于wpf - 使用不同的画笔动画颜色属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19481197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com