Aller au contenu

Animations

Des keyframes, une durée, un nombre de répétitions. Déclenchées par un style ou par le code.

Cue="0%"Cue="50%"Cue="100%" Opacity 1Opacity 0.3Opacity 1 interpoléinterpolé ← Duration →

Une animation = des KeyFrames placées sur une frise (Cue) ; Avalonia calcule les valeurs intermédiaires.

Animation en boucle

Aperçu · en direct
En ligne
<Style Selector="Ellipse.pulse">
  <Style.Animations>
    <Animation Duration="0:0:1" IterationCount="Infinite" PlaybackDirection="Alternate">
      <KeyFrame Cue="0%">
        <Setter Property="Opacity" Value="1" />
      </KeyFrame>
      <KeyFrame Cue="100%">
        <Setter Property="Opacity" Value="0.3" />
      </KeyFrame>
    </Animation>
  </Style.Animations>
</Style>

<Ellipse Classes="pulse" Width="16" Height="16" Fill="#10B981" />

Rotation continue (spinner)

Aperçu · en direct
<Style Selector="PathIcon.spin">
  <Style.Animations>
    <Animation Duration="0:0:1" IterationCount="Infinite">
      <KeyFrame Cue="0%"><Setter Property="RotateTransform.Angle" Value="0" /></KeyFrame>
      <KeyFrame Cue="100%"><Setter Property="RotateTransform.Angle" Value="360" /></KeyFrame>
    </Animation>
  </Style.Animations>
</Style>

<PathIcon Classes="spin" Data="M12 2a10 10 0 1 0 10 10h-3a7 7 0 1 1-7-7z" Width="24" Height="24" />

Apparition à l'affichage

Aperçu · fondu + glissement vers le haut
123
<Style Selector="Border.apparition">
  <Style.Animations>
    <Animation Duration="0:0:0.4" FillMode="Forward" Easing="CubicEaseOut">
      <KeyFrame Cue="0%">
        <Setter Property="Opacity" Value="0" />
        <Setter Property="TranslateTransform.Y" Value="20" />
      </KeyFrame>
      <KeyFrame Cue="100%">
        <Setter Property="Opacity" Value="1" />
        <Setter Property="TranslateTransform.Y" Value="0" />
      </KeyFrame>
    </Animation>
  </Style.Animations>
</Style>

FillMode="Forward" garde la valeur finale après l'animation.

Plusieurs étapes

<Animation Duration="0:0:0.6" IterationCount="1">
  <KeyFrame Cue="0%"><Setter Property="TranslateTransform.X" Value="0" /></KeyFrame>
  <KeyFrame Cue="20%"><Setter Property="TranslateTransform.X" Value="-10" /></KeyFrame>
  <KeyFrame Cue="40%"><Setter Property="TranslateTransform.X" Value="10" /></KeyFrame>
  <KeyFrame Cue="60%"><Setter Property="TranslateTransform.X" Value="-6" /></KeyFrame>
  <KeyFrame Cue="80%"><Setter Property="TranslateTransform.X" Value="6" /></KeyFrame>
  <KeyFrame Cue="100%"><Setter Property="TranslateTransform.X" Value="0" /></KeyFrame>
</Animation>

Effet « secousse », parfait pour un champ en erreur :

Aperçu · survolez ou cliquez dans le champ
E-mail invalide.
<Style Selector="TextBox:error">
  <Style.Animations>
    <!-- animation ci-dessus -->
  </Style.Animations>
</Style>

Options de l'animation

Propriété Valeurs
Duration 0:0:0.5 (hⓂs)
Delay attente avant le départ
IterationCount 1, 3, Infinite
PlaybackDirection Normal, Reverse, Alternate, AlternateReverse
FillMode None, Forward, Backward, Both
Easing voir Transitions
SpeedRatio 2 = deux fois plus vite
Aperçu · PlaybackDirection, en direct
Normal : 0 → 100, recommence à 0
Reverse : 100 → 0
Alternate : aller-retour

Démarrer / arrêter avec une classe

Une animation de style tourne tant que le sélecteur correspond.

<Style Selector="Border.chargement">
  <Style.Animations>
    <Animation Duration="0:0:0.8" IterationCount="Infinite" PlaybackDirection="Alternate">
      <KeyFrame Cue="0%"><Setter Property="Opacity" Value="0.4" /></KeyFrame>
      <KeyFrame Cue="100%"><Setter Property="Opacity" Value="1" /></KeyFrame>
    </Animation>
  </Style.Animations>
</Style>

<Border Classes.chargement="{Binding EstEnChargement}" />

v12 : PlaybackBehavior

Le comportement des animations déclenchées par un style a été revu. Pour retrouver celui d'Avalonia 11, définissez PlaybackBehavior="Always" sur l'Animation.

Squelette de chargement (skeleton)

Aperçu · en direct
<Style Selector="Border.skeleton">
  <Setter Property="Background" Value="#E5E7EB" />
  <Setter Property="CornerRadius" Value="4" />
  <Style.Animations>
    <Animation Duration="0:0:1.2" IterationCount="Infinite" PlaybackDirection="Alternate">
      <KeyFrame Cue="0%"><Setter Property="Opacity" Value="1" /></KeyFrame>
      <KeyFrame Cue="100%"><Setter Property="Opacity" Value="0.45" /></KeyFrame>
    </Animation>
  </Style.Animations>
</Style>

<StackPanel Spacing="8" Width="260">
  <Border Classes="skeleton" Height="16" Width="180" HorizontalAlignment="Left" />
  <Border Classes="skeleton" Height="12" />
  <Border Classes="skeleton" Height="12" Width="220" HorizontalAlignment="Left" />
</StackPanel>

Animer depuis le code

using Avalonia.Animation;
using Avalonia.Animation.Easings;
using Avalonia.Styling;

var animation = new Animation
{
    Duration = TimeSpan.FromMilliseconds(300),
    Easing = new CubicEaseOut(),
    FillMode = FillMode.Forward,
    Children =
    {
        new KeyFrame { Cue = new Cue(0), Setters = { new Setter(OpacityProperty, 0d) } },
        new KeyFrame { Cue = new Cue(1), Setters = { new Setter(OpacityProperty, 1d) } }
    }
};

await animation.RunAsync(monControle);   // attend la fin

Annuler

using var cts = new CancellationTokenSource();
var tache = animationInfinie.RunAsync(spinner, cts.Token);
// …
cts.Cancel();

Transformations animables

Pour animer une transformation, utilisez les propriétés « raccourcis » :

Aperçu · les 4 transformations, en boucle
Translate
Scale
Rotate
Skew
Propriété Effet
TranslateTransform.X / .Y déplacement
ScaleTransform.ScaleX / .ScaleY échelle
RotateTransform.Angle rotation
SkewTransform.AngleX / .AngleY inclinaison

Transition ou animation ?

Transition : réagir à un changement d'état (survol, sélection). Animation : mouvement autonome (boucle, apparition, attirer l'attention).