Otro La forma posible es crear una clase de animación personalizada que anima los pinceles. Encontré una manera simple de hacerlo creando una clase, derivada de AnimationTimeline
. Podemos anular algunos miembros en la clase personalizada, entre otras cosas, el AnimationTimeline.GetCurrentValue
method. Devuelve un valor que depende del progreso de la animación y del valor inicial y final.
La manera más simple es crear un VisualBrush
y realizar un fundido cruzado con el valor final con la propiedad Opacity
en un control secundario. El resultado es una clase como la siguiente:
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));
}
Usted puede usarlo como siempre en 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>
o en el código detrás:
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();
También es posible extender el BrushAnimation
con sobrecargas de constructor, etc., por lo que parece un tipo de animación dado por .NET.
Eso funciona bien si sé qué tipo de cepillo esperar. Pero en mi caso, puede haber color sólido, gradiente lineal o gradiente radial dependiendo de la configuración de los sensores. ¿Hay una forma general de cepillar la animación (independientemente del tipo de pincel)? –