Animation is a capability of Silverlight 1.1. Animation allows you to create truly dynamic user interfaces. It's often used to apply effects -- for example, icons that grow when you move over them, logos that spin, text that scrolls into view, and so on. Animations are a core part of the Silverlight model. That means you don't need to use timers and event-handling code to put them into action. Instead, you can create them declaratively, configure them using one of a handful of classes, and put them into action without writing a single line of C# code.
Silverlight animation is a scaled-down version of the WPF animation system. In order to understand Silverlight animation, you need to understand the following key rules:
- Silverlight performs time-based animation. Thus, you set the initial state, the final state, and the duration of your animation. Silverlight calculates the frame rate.
- Silverlight uses a property-based animation model. That means a Silverlight animation can do only one thing: modify the value of a property over an interval of time. This sounds like a significant limitation (and it many ways, it is), but there's a surprisingly large range of effects you can create by simply modifying properties.
- To animate a property, you need to have an animation class that supports its data type. For example, if you want to change a property that uses the double data type (which is one of the most common scenarios), you must use the DoubleAnimation class. If you want to modify the color that's used to paint the background of your Canvas, you need to use the ColorAnimation class.
Silverlight has relatively few animation classes, so you're limited in the data types you can use. At present, you can use animations to modify properties with the following data types: double Color, and Point.
As a rule of thumb, the property-based animation is a great way to add dynamic effects to an otherwise ordinary application (like buttons that glow, pictures that expand when you move over them, and so on). However, if you need to use animations as part of the core purpose of your application and you want them to continue running over the lifetime of your application, you probably need something more flexible and more powerful. For example, if you're creating a basic arcade game or using complex physics calculations to model collisions, you'll need greater control over the animation. Unfortunately, Silverlight doesn't have an option for frame-based animation, so you'll be forced to create this sort of application the old-fashioned way -- by looping endlessly, being careful to modify your visuals and check for user input after each iteration. You can see an example of this technique with the ball collision simulator at http://bubblemark.com.
Defining an Animation
Creating an animation is a multistep process. You need to create three separate ingredients: an animation object to perform your animation, a storyboard to manage your animation, and an event trigger to start your storyboard. In the following sections, you'll tackle each of these steps.
The Animation Class
There are actually two types of animation classes in Silverlight. Each type of animation uses a different strategy for varying a property value.
- Linear interpolation: With linear interpretation, the property value varies smoothly and continuously over the duration of the animation. Examples include DoubleAnimation, PointAnimation, and ColorAnimation.
- Key frame animation: With key frame animation, values can jump abruptly from one value to another, or they can combine jumps and periods of linear interpolation. Examples include ColorAnimationUsingKeyFrames, DoubleAnimationUsingKeyFrames, and PointAnimationUsingKeyFrames.
In this article, you'll focus exclusively on the most commonly used animation class: the DoubleAnimation class. It uses linear interpolation to change a double from a starting value to its ending value. Animations are defined using XAML markup. Although the animation classes aren't elements, they can still be created with the same XAML syntax. For example, here's the markup required to create a DoubleAnimation:
DoubleAnimation From="160" To="300" Duration="0:0:5"></DoubleAnimation>
This animation lasts 5 seconds (as indicated by the Durationproperty, which takes a time value in the format Hours:Minutes:Seconds.FractionalSeconds). While the animation is running, it changes the target value from 160 to 300. Because the DoubleAnimation uses linear interpolation, this change takes place smoothly and continuously.
There's one important detail that's missing from this markup. The animation indicates how the property will be changed, but it doesn't indicate what property to use. This detail is supplied by another ingredient, which is represented by the Storyboard class.