Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #76 - Animating objects with DoubleAnimation.

In Silverlight you can use the Storyboard control to animate properties of an object. It can be used against properties of type double, Color or Point. For example, you can animate changing the double type property Opacity of a Rectangle over time. A Storyboard can be declared in your XAML but it must be placed as a resource. For example, the following Storyboard is declared as a resource of the Canvas object:

<Canvas>                 
    <Canvas.Resources>
        <Storyboard x:Name="MyRect">
        </Storyboard>
    </Canvas.Resources>
</Canvas>

The Storyboard control provides the user with two options for animating:

  1. DoubleAnimation - Uses a From/To direction.
  2. DoubleAnimationUsingKeyFrames - This is a container for a set of key frames that will determine the value at a given set of points along a time line. We will look at this option in our next tip of the day.

DoubleAnimation.

As stated above, this uses a From/To direction that takes the targeting property from one value to another. Let's take a close look at some of the properties DoubleAnimation provides:

  1. Storyboard.TargetProperty - Use this to target the property you wish to animate. In should be in the format UIElement.Property.
  2. Storyboard.TargetName - Use this to target the object you want to animate.
  3. Duration - Use this property to specify how long the animation should take. Format =  [days.]hours:minutes:seconds.
  4. From/To - The start and end values you want the property to be set to. By Default From = 1.0 and does not have to be set unless you want it to be a different value.
  5. AutoReverse - This will cause the animation to reverse course once it's completed.
  6. RepeatBehavior - Set this to "Forever" to cause it to repeat non-stop. You can also set this to a duration (format =  [days.]hours:minutes:seconds) or an iteration count.

The following below is an example of a DoubleAnimation where we target an image's opacity fading the image in and out non-stop.

<Canvas>                 
    <Canvas.Resources>
        <Storyboard x:Name="CatSB">
            <DoubleAnimation Storyboard.TargetName="MyCat" Storyboard.TargetProperty="UIElement.Opacity"
                             Duration="00:00:02" From="1.0" To="0.0" 
                             AutoReverse="True"  RepeatBehavior="Forever">                        
            </DoubleAnimation>
        </Storyboard>
    </Canvas.Resources>
    <Image Source="cat.jpg" x:Name="MyCat" >              
    </Image>
</Canvas>

To start the Storyboard we call CatSB.Begin(); from our code.

Live Demo:

Thank you,
--Mike Snow

Subscribe in a reader

Comments

Microsoft Weblogs said:

In Silverlight you can use the Storyboard control to animate properties of an object. It can be used

# December 1, 2008 2:55 PM

Community Blogs said:

In this issue: Alex Golesh, Silverlight SDK, John Stockton, Page Brooks(2), Mike Snow(2), Terence Tsang

# December 2, 2008 1:20 AM

2008 December 02 - Links for today « My (almost) Daily Links said:

Pingback from  2008 December 02 - Links for today &laquo; My (almost) Daily Links

# December 2, 2008 5:12 AM

Dew Drop - December 2, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - December 2, 2008 | Alvin Ashcraft's Morning Dew

# December 2, 2008 8:45 AM

Roc1 said:

Is the cat image supposed to flicker as it fades in and out?  I've noticed the same jittery behavior with other Silverlight animations on other sites.  Is this a known issue or is there just something wrong with my SL install?

Thanks, RC

# December 2, 2008 10:00 AM

mike.snow said:

Roc1- I viewed it on multiple machines but was unable to see a flicker. The animation was smooth. How is the performance of your machine?

# December 2, 2008 10:06 AM

Roc1 said:

Hi Mike,

I'm using a dual core Intel with lots of RAM.  I see flickering in FF3 and IE7.

Silverlight 2.0.31005.0 is installed.

If you can connect me with someone on the Silverlight Test team, I'll be happy to work with them.

robert.g.claypool * AT * live.com

# December 2, 2008 10:20 AM

mike.snow said:

Thanks for your contact. I have sent you and the SL QA lead an email. Let me know what you find out!

# December 2, 2008 10:55 AM

mike.snow said:

Thanks for your contact. I have sent you and the SL QA lead an email. Let me know what you find out!

# December 2, 2008 10:55 AM

Silverlight Tips of the Day - Blog by Mike Snow said:

In Tip of the Day #77 I explored using DoubleAnimation to animate a given controls property value using

# December 5, 2008 3:42 PM

Roc1 said:

Mike -

We have almost tracked down the animation bug.  

I recently installed www.demoforge.com/dfmirage.htm and animations are now very smooth. The problem must have been the old driver or one of it's settings.

# December 5, 2008 6:54 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:24 AM

Visual Web Developer Team Blog said:

This link provides a complete Tips of the Day Summary Outline - silverlight.net/.../silverlight-tips-of-the-day-summary-outline.aspx

# January 8, 2009 6:37 PM

jin_u as blog » Blog Archive » ??????????????? ??? ?????? said:

Pingback from  jin_u as blog  &raquo; Blog Archive   &raquo; ??????????????? ??? ??????

# January 14, 2009 7:54 PM