January 28, 2008
Silverlight AnimationMatthew MacDonald and Mario Szpuszta
Silverlight animation lets create truly dynamic user interfaces
Matthew MacDonald and Mario Szpuszta are the authors of Pro ASP.NET 3.5 in C# 2008, Second Edition, from which this article is adapted. Copyright Apress All rights reserved.
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.
Animation Basics
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 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.
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.
|
|
||||||||||||||||||||||||||||||
|
|
|
|