Animation pinceau à pinceau

J’ai réussi à savoir comment faire une animation WPF – transition entre deux couleurs.

Cela s’appelle ColorAnimation et fonctionne bien.

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

J’utilise ceci pour animer l’arrière-plan de mon contrôle utilisateur. L’arrière-plan de mes contrôles est SolidColorBrush . Récemment, j’ai changé pour LinearGradientBrush . Maintenant, je ne peux plus utiliser mon animation.

J’ai besoin d’animation de pinceau à pinceau, pas de couleur à couleur. Et la meilleure option est le type de pinceau abstrait, qui inclut SolidColor, LinearGradient, etc., afin que je puisse animer, par exemple, de SolidColorBrush à LinearGradientBrush . Est-ce que c’est possible? Je vous remercie.

Une autre possibilité consiste à créer une classe d’animation personnalisée qui animera les pinceaux. J’ai trouvé un moyen simple de le faire en créant une classe, dérivée de AnimationTimeline . Nous pouvons remplacer certains membres de la classe personnalisée, notamment la méthode AnimationTimeline.GetCurrentValue . Elle retourne une valeur qui dépend de la progression de l’animation et des valeurs de début et de fin.

Le moyen le plus simple consiste à créer un VisualBrush et à VisualBrush un fondu enchaîné au début avec la valeur de fin avec la propriété Opacity sur un contrôle enfant. Le résultat est une classe comme celle-ci:

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

Vous pouvez l’utiliser comme toujours en XAML:

                

ou dans le code derrière:

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

Il est également possible d’étendre BrushAnimation avec des surcharges de constructeur, etc., de sorte qu’il ressemble à un type d’animation donné .NET.

Il vous suffit d’utiliser l’animation de couleur sur les arrêts de dégradé du pinceau à dégradé. Voici un exemple qui anime un dégradé de rectangle à l’aide d’un scénarimage.

                             

Vous pouvez animer la couleur du pinceau si vous avez un style de modèle dans lequel vous lui donnez un nom, comme suit:

                

Tel que repris de MSDN