Int16Animation Class
Assembly: PresentationCore (in PresentationCore.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
An animation updates the value of a property over a period of time. An animation effect can be subtle, such as moving a Shape a few pixels left and right, or dramatic, such as enlarging an object to 200 times its original size while spinning it and changing its color. To create an animation in Windows Presentation Foundation (WPF), you associate an animation with an object's property value.
Target Values
The Int16Animation class creates a transition between two target values. To set its target values, use its From, To, and By properties. The following table summarizes how the From, To, and By properties may be used together or separately to determine an animation's target values.
Properties specified | Resulting behavior |
|---|---|
The animation progresses from the value specified by the From property to the base value of the property being animated or to a previous animation's output value, depending on how the previous animation is configured. | |
The animation progresses from the value specified by the From property to the value specified by the To property. | |
The animation progresses from the value specified by the From property to the value specified by the sum of the From and By properties. | |
The animation progresses from the animated property's base value or a previous animation's output value to the value specified by the To property. | |
The animation progresses from the base value of the property being animated or a previous animation's output value to the sum of that value and the value specified by the By property. |
Note: |
|---|
If you set both the To and By properties, the To property takes precedence and the By property is ignored. |
To use other interpolation methods or animate between more than two target values, use a Int16AnimationUsingKeyFrames object.
For information about applying multiple animations to a single property, see Key-Frame Animations Overview.
Freezable Features
Because the Int16Animation class inherits from Freezable, Int16Animation objects gain several special features, which include the following: they can be declared as resources, shared among multiple objects, made read-only to improve performance, cloned, and made thread-safe. For more information about the different features provided by Freezable objects, see the Freezable Objects Overview.
This example shows how to use a Storyboard to animate properties. To animate a property by using a Storyboard, create an animation for each property that you want to animate and also create a Storyboard to contain the animations.
The type of property determines the type of animation to use. For example, to animate a property that takes Double values, use a DoubleAnimation. The TargetName and TargetProperty attached properties specify the object and property to which the animation is applied.
To start a storyboard in Extensible Application Markup Language (XAML), use a BeginStoryboard action and an EventTrigger. The EventTrigger begins the BeginStoryboard action when the event that is specified by its RoutedEvent property occurs. The BeginStoryboard action starts the Storyboard.
The following example uses Storyboard objects to animate two Button controls. To make the first button change in size, its Width is animated. To make the second button change color, the Color property of the SolidColorBrush is used to set the Background of the button that is animated.
<!-- StoryboardExample.xaml Uses storyboards to animate properties. --> <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowTitle="Animate Properties with Storyboards"> <Border Background="White"> <StackPanel Margin="30" HorizontalAlignment="Left" MinWidth="500"> <TextBlock>Storyboard Animation Example</TextBlock> <!-- The width of this button is animated. --> <Button Name="myWidthAnimatedButton" Height="30" Width="200" HorizontalAlignment="Left"> A Button <Button.Triggers> <!-- Animates the width of the first button from 200 to 300. --> <EventTrigger RoutedEvent="Button.Click"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="myWidthAnimatedButton" Storyboard.TargetProperty="Width" From="200" To="300" Duration="0:0:3" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Button.Triggers> </Button> <!-- The color of the brush used to paint this button is animated. --> <Button Height="30" Width="200" HorizontalAlignment="Left">Another Button <Button.Background> <SolidColorBrush x:Name="myAnimatedBrush" Color="Blue" /> </Button.Background> <Button.Triggers> <!-- Animates the color of the brush used to paint the second button from red to blue . --> <EventTrigger RoutedEvent="Button.Click"> <BeginStoryboard> <Storyboard> <ColorAnimation Storyboard.TargetName="myAnimatedBrush" Storyboard.TargetProperty="Color" From="Red" To="Blue" Duration="0:0:7" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Button.Triggers> </Button> </StackPanel> </Border> </Page>
Note: |
|---|
Although animations can target both a FrameworkElement object, such as a Control or Panel, and a Freezable object, such as a Brush or Transform, only framework elements have a Name property. To assign a name to a freezable so that it can be targeted by an animation, use the x:Name Attribute, as the previous example shows. |
If you use code, you must create a NameScope for a FrameworkElement and register the names of the objects to animate with that FrameworkElement. To start the animations in code, use a BeginStoryboard action with an EventTrigger. Optionally, you can use an event handler and the Begin method of Storyboard. The following example shows how to use the Begin method.
Imports System Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Media Imports System.Windows.Media.Animation Namespace SDKSample ' Uses a storyboard to animate the properties ' of two buttons. Public Class StoryboardExample Inherits Page Private Dim WithEvents myWidthAnimatedButton As Button Private Dim WithEvents myColorAnimatedButton As Button Private Dim myWidthAnimatedButtonStoryboard As Storyboard Private Dim myColorAnimatedButtonStoryboard As Storyboard Public Sub New() ' Create a name scope for the page. NameScope.SetNameScope(Me, New NameScope()) Me.WindowTitle = "Animate Properties using Storyboards" Dim myStackPanel As New StackPanel() myStackPanel.MinWidth = 500 myStackPanel.Margin = New Thickness(30) myStackPanel.HorizontalAlignment = HorizontalAlignment.Left Dim myTextBlock As New TextBlock() myTextBlock.Text = "Storyboard Animation Example" myStackPanel.Children.Add(myTextBlock) ' ' Create and animate the first button. ' ' Create a button. myWidthAnimatedButton = New Button() myWidthAnimatedButton.Height = 30 myWidthAnimatedButton.Width = 200 myWidthAnimatedButton.HorizontalAlignment = HorizontalAlignment.Left myWidthAnimatedButton.Content = "A Button" ' Set the Name of the button so that it can be referred ' to in the storyboard that's created later. ' The ID doesn't have to match the variable name; ' it can be any unique identifier. myWidthAnimatedButton.Name = "myWidthAnimatedButton" ' Register the name with the page to which the button belongs. Me.RegisterName(myWidthAnimatedButton.Name, myWidthAnimatedButton) ' Create a DoubleAnimation to animate the width of the button. Dim myDoubleAnimation As New DoubleAnimation() myDoubleAnimation.From = 200 myDoubleAnimation.To = 300 myDoubleAnimation.Duration = New Duration(TimeSpan.FromMilliseconds(3000)) ' Configure the animation to target the button's Width property. Storyboard.SetTargetName(myDoubleAnimation, myWidthAnimatedButton.Name) Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Button.WidthProperty)) ' Create a storyboard to contain the animation. myWidthAnimatedButtonStoryboard = New Storyboard() myWidthAnimatedButtonStoryboard.Children.Add(myDoubleAnimation) myStackPanel.Children.Add(myWidthAnimatedButton) ' ' Create and animate the second button. ' ' Create a second button. myColorAnimatedButton = New Button() myColorAnimatedButton.Height = 30 myColorAnimatedButton.Width = 200 myColorAnimatedButton.HorizontalAlignment = HorizontalAlignment.Left myColorAnimatedButton.Content = "Another Button" ' Create a SolidColorBrush to paint the button's background. Dim myBackgroundBrush As New SolidColorBrush() myBackgroundBrush.Color = Colors.Blue ' Because a Brush isn't a FrameworkElement, it doesn't ' have a Name property to set. Instead, you just ' register a name for the SolidColorBrush with ' the page where it's used. Me.RegisterName("myAnimatedBrush", myBackgroundBrush) ' Use the brush to paint the background of the button. myColorAnimatedButton.Background = myBackgroundBrush ' Create a ColorAnimation to animate the button's background. Dim myColorAnimation As New ColorAnimation() myColorAnimation.From = Colors.Red myColorAnimation.To = Colors.Blue myColorAnimation.Duration = New Duration(TimeSpan.FromMilliseconds(7000)) ' Configure the animation to target the brush's Color property. Storyboard.SetTargetName(myColorAnimation, "myAnimatedBrush") Storyboard.SetTargetProperty(myColorAnimation, New PropertyPath(SolidColorBrush.ColorProperty)) ' Create a storyboard to contain the animation. myColorAnimatedButtonStoryboard = New Storyboard() myColorAnimatedButtonStoryboard.Children.Add(myColorAnimation) myStackPanel.Children.Add(myColorAnimatedButton) Me.Content = myStackPanel End Sub ' Start the animation when the button is clicked. Private Sub myWidthAnimatedButton_Loaded(ByVal sender as object, ByVal args as RoutedEventArgs) Handles myWidthAnimatedButton.Click myWidthAnimatedButtonStoryboard.Begin(myWidthAnimatedButton) End Sub ' Start the animation when the button is clicked. Private Sub myColorAnimatedButton_Loaded(ByVal sender as object, ByVal args as RoutedEventArgs) Handles myColorAnimatedButton.Click myColorAnimatedButtonStoryboard.Begin(myColorAnimatedButton) End Sub End Class End Namespace
For the complete sample, see Property Animation Sample. For more information about animation and storyboards, see Animation Overview.
If you use code, you are not limited to using Storyboard objects in order to animate properties. For more information and examples, see How to: Animate a Property Without Using a Storyboard and How to: Animate a Property by Using an AnimationClock.
More Code
| How to: Animate a Property Without Using a Storyboard | This example shows one way to apply an animation to a property without using a Storyboard. |
| How to: Control an Animation using From, To, and By | A "From/To/By" or "basic animation" creates a transition between two target values (see Animation Overview for an introduction to different types of animations). To set the target values of a basic animation, use its From, To, and By properties. The following table summarizes how the From, To, and By properties may be used together or separately to determine an animation's target values. |
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.Int16Animation
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: