Animations¶
Des keyframes, une durée, un nombre de répétitions. Déclenchées par un style ou par le code.
Une animation = des KeyFrames placées sur une frise (Cue) ; Avalonia calcule les valeurs intermédiaires.
Animation en boucle¶
<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)¶
<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¶
<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 :
<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 |
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 |
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)¶
<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 » :
| 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).