Classe Int16AnimationUsingKeyFrames
Aggiornamento: novembre 2007
Spazio dei nomi: System.Windows.Media.AnimationAssembly: PresentationCore (in PresentationCore.dll)
XMLNS per XAML: http://schemas.microsoft.com/winfx/xaml/presentation
[ContentPropertyAttribute("KeyFrames")] public class Int16AnimationUsingKeyFrames : Int16AnimationBase, IKeyFrameAnimation, IAddChild
/** @attribute ContentPropertyAttribute("KeyFrames") */
public class Int16AnimationUsingKeyFrames extends Int16AnimationBase implements IKeyFrameAnimation,
IAddChild
public class Int16AnimationUsingKeyFrames extends Int16AnimationBase implements IKeyFrameAnimation, IAddChild
<Int16AnimationUsingKeyFrames> KeyFrames </Int16AnimationUsingKeyFrames>
I valori di destinazione di un'animazione del fotogramma chiave sono definiti dalla relativa proprietà KeyFrames, che contiene un insieme di oggetti Int16KeyFrame. Ciascun oggetto Int16KeyFrame definisce un segmento dell'animazione con il proprio oggetto Value e KeyTime di destinazione. Quando l'animazione è in esecuzione, avanza da un valore chiave al successivo in base alle chiavi temporali specificate.
Sono disponibili tre tipi di classi Int16KeyFrame, una per ogni metodo di interpolazione supportato: LinearInt16KeyFrame, DiscreteInt16KeyFrame e SplineInt16KeyFrame.
A differenza di Int16Animation, Int16AnimationUsingKeyFrames può disporre di più di due valori di destinazione. È anche possibile controllare il metodo di interpolazione di singoli segmenti di Int16KeyFrame.
In questo esempio viene illustrato come animare il valore di una proprietà che accetta un oggetto Double utilizzando frame chiave.
Nell'esempio riportato di seguito viene spostato un rettangolo attraverso una schermata. Nell'esempio viene utilizzata la classe DoubleAnimationUsingKeyFrames per animare la proprietà X di un oggetto TranslateTransform applicata a un oggetto Rectangle. Questa animazione, che viene ripetuta all'infinito, utilizza tre frame chiave nel modo seguente:
Durante i primi tre secondi, viene utilizzata un'istanza della classe LinearDoubleKeyFrame per spostare il rettangolo lungo un percorso a una velocità costante dalla posizione iniziale alla posizione 500. I frame chiave lineari come LinearDoubleKeyFrame creano una transizione lineare uniforme tra valori.
Alla fine del quarto secondo, viene utilizzata un'istanza della classe DiscreteDoubleKeyFrame per spostare improvvisamente il rettangolo alla posizione successiva. Frame chiave discreti come DiscreteDoubleKeyFrame creano salti improvvisi tra valori. In questo esempio, il rettangolo si trova alla posizione iniziale, quindi viene improvvisamente visualizzato alla posizione 500.
Nei due secondi finali, viene utilizzata un'istanza della classe SplineDoubleKeyFrame per riportare il rettangolo nella posizione iniziale. I fotogrammi chiave Spline come SplineDoubleKeyFrame creano una transizione variabile tra i valori a seconda del valore della proprietà KeySpline. In questo esempio, il rettangolo inizia a spostarsi lentamente, quindi accelera in modo esponenziale verso la fine dell'intervallo di tempo.
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Shapes; using System.Windows.Media.Animation; using System.Windows.Media; namespace Microsoft.Samples.KeyFrameExamples { /// <summary> /// This example shows how to use the DoubleAnimationUsingKeyFrames class to /// animate the position of an object. /// Key frame animations enable you to create complex animations /// by specifying multiple destination values /// and controlling the animation's interpolation method. /// </summary> public class AltDoubleAnimationUsingKeyFramesExample : Page { public AltDoubleAnimationUsingKeyFramesExample() { Title = "DoubleAnimationUsingKeyFrames Example"; Background = Brushes.White; Margin = new Thickness(20); // Create a NameScope for this page so that // Storyboards can be used. NameScope.SetNameScope(this, new NameScope()); // Create a rectangle. Rectangle aRectangle = new Rectangle(); aRectangle.Width = 100; aRectangle.Height = 100; aRectangle.Stroke = Brushes.Black; aRectangle.StrokeThickness = 5; // Create a Canvas to contain and // position the rectangle. Canvas containerCanvas = new Canvas(); containerCanvas.Width = 610; containerCanvas.Height = 300; containerCanvas.Children.Add(aRectangle); Canvas.SetTop(aRectangle, 100); Canvas.SetLeft(aRectangle, 10); // Create a TranslateTransform to // move the rectangle. TranslateTransform animatedTranslateTransform = new TranslateTransform(); aRectangle.RenderTransform = animatedTranslateTransform; // Assign the TranslateTransform a name so that // it can be targeted by a Storyboard. this.RegisterName( "AnimatedTranslateTransform", animatedTranslateTransform); // Create a DoubleAnimationUsingKeyFrames to // animate the TranslateTransform. DoubleAnimationUsingKeyFrames translationAnimation = new DoubleAnimationUsingKeyFrames(); translationAnimation.Duration = TimeSpan.FromSeconds(6); // Animate from the starting position to 500 // over the first second using linear // interpolation. translationAnimation.KeyFrames.Add( new LinearDoubleKeyFrame( 500, // Target value (KeyValue) KeyTime.FromTimeSpan(TimeSpan.FromSeconds(3))) // KeyTime ); // Animate from 500 (the value of the previous key frame) // to 400 at 4 seconds using discrete interpolation. // Because the interpolation is discrete, the rectangle will appear // to "jump" from 500 to 400. translationAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame( 400, // Target value (KeyValue) KeyTime.FromTimeSpan(TimeSpan.FromSeconds(4))) // KeyTime ); // Animate from 400 (the value of the previous key frame) to 0 // over two seconds, starting at 4 seconds (the key time of the // last key frame) and ending at 6 seconds. translationAnimation.KeyFrames.Add( new SplineDoubleKeyFrame( 0, // Target value (KeyValue) KeyTime.FromTimeSpan(TimeSpan.FromSeconds(6)), // KeyTime new KeySpline(0.6,0.0,0.9,0.0) // KeySpline ) ); // Set the animation to repeat forever. translationAnimation.RepeatBehavior = RepeatBehavior.Forever; // Set the animation to target the X property // of the object named "AnimatedTranslateTransform." Storyboard.SetTargetName(translationAnimation, "AnimatedTranslateTransform"); Storyboard.SetTargetProperty( translationAnimation, new PropertyPath(TranslateTransform.XProperty)); // Create a storyboard to apply the animation. Storyboard translationStoryboard = new Storyboard(); translationStoryboard.Children.Add(translationAnimation); // Start the storyboard after the rectangle loads. aRectangle.Loaded += delegate(object sender, RoutedEventArgs e) { translationStoryboard.Begin(this); }; Content = containerCanvas; } } }
<!-- This example shows how to use the DoubleAnimationUsingKeyFrames to animate the position of an object. Key frame animations enable you to create complex animations by specifying multiple destination values and controlling the animation's interpolation method. --> <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="DoubleAnimationUsingKeyFrames Example" Background="White" Margin="20"> <Canvas Width="610" Height="300"> <!-- The position of this rectangle is animated using a key frame animation. --> <Rectangle Canvas.Top="100" Canvas.Left="10" Height="100" Width="100" Stroke="Black" StrokeThickness="5"> <Rectangle.RenderTransform> <TranslateTransform x:Name="AnimatedTranslateTransform" /> </Rectangle.RenderTransform> <Rectangle.Triggers> <EventTrigger RoutedEvent="Rectangle.Loaded"> <BeginStoryboard> <Storyboard> <!-- Animate the TranslateTransform.X property using 3 KeyFrames which animates the rectangle along a straight line. This animation repeats indefinitely. --> <DoubleAnimationUsingKeyFrames Storyboard.TargetName="AnimatedTranslateTransform" Storyboard.TargetProperty="X" Duration="0:0:6" RepeatBehavior="Forever"> <!-- Using a LinearDoubleKeyFrame, the rectangle moves steadily from its starting position to 500 over the first 3 seconds. --> <LinearDoubleKeyFrame Value="500" KeyTime="0:0:3" /> <!-- Using a DiscreteDoubleKeyFrame, the rectangle suddenly appears at 400 after the fourth second of the animation. --> <DiscreteDoubleKeyFrame Value="400" KeyTime="0:0:4" /> <!-- Using a SplineDoubleKeyFrame, the rectangle moves back to its starting point. The animation starts out slowly at first and then speeds up. This KeyFrame ends after the 6th second. --> <SplineDoubleKeyFrame KeySpline="0.6,0.0 0.9,0.00" Value="0" KeyTime="0:0:6" /> </DoubleAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger> </Rectangle.Triggers> </Rectangle> </Canvas> </Page>
Per l'esempio completo, vedere Esempio di animazione con fotogrammi chiave.
Per coerenza con altri esempi di animazione, nelle versioni del codice di questo esempio viene utilizzato un oggetto Storyboard per applicare l'oggetto DoubleAnimationUsingKeyFrames. In alternativa, quando si applica una sola animazione al codice, è più semplice utilizzare il metodo BeginAnimation, anziché un oggetto Storyboard. Per un esempio, vedere Procedura: animare una proprietà senza utilizzare uno storyboard.
System.Windows.Threading.DispatcherObject
System.Windows.DependencyObject
System.Windows.Freezable
System.Windows.Media.Animation.Animatable
System.Windows.Media.Animation.Timeline
System.Windows.Media.Animation.AnimationTimeline
System.Windows.Media.Animation.Int16AnimationBase
System.Windows.Media.Animation.Int16AnimationUsingKeyFrames
Windows Vista
.NET Framework e .NET Compact Framework non supportano tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.