Animations in XAML
 
Published: 25 Oct 2006
Abstract
Animations are an important aspect of XAML and Windows Presentation Foundation that show their power to all developers who have written an animation for older graphic user interfaces. In XAML, animations are easy to declare and customize. In this article Keyvan introduces animations in XAML and walks through the fundamentals of declaring different types of animations.
by Keyvan Nayyeri
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 41391/ 79

Introduction

If you have worked with old (for now, it is better to say "current") graphic engines to design a dynamic animation for your user interface, surely you know it can be a time consuming task which needs hard coding.

The next generation of user interfaces, which comes with Windows Vista and Windows Presentation Foundation and uses XAML in its core, has made this process much easier.

You can use XAML codes to write various types of animations in a short time and enjoy them.

In this article I want to introduce you to animations in XAML; it is a great markup language for designing your UI in Windows Vista and .NET 3.0.  My primary goal is to walk through common animation types.  The way to declare and use animations has changed a bit during the development of XAML and Windows Presentation Foundation in the last year, but it seems to be stable now.

Before reading this article, note that I will try to show animation results in a few snapshots, but they cannot represent the actual output.  The best way to see the output of examples is to run them yourself.

Overview

The process of writing a common animation, such as changing colors, transforming, etc., is really easy.  You can start an animation when a specific event occurs.  To do this you can simply use a trigger which runs the animation when a specific RoutedEvent occurs.  XAML uses Storyboard elements to create an animation.  Each Storyboard consists of one of the different animation type elements.

One of the most important aspects of animations is timing.  It means developers should calculate the time that an animation moves from one state to another and the entire time that an animation takes.  This is not an easy task to accomplish in the current graphic user interfaces.  However, Windows Presentation Foundation solves this and makes it easy for developers so you should not worry about it at all.

Storyboards

Storyboard elements can be declared to contain animations.  Storyboard elements should be nested in the BeginStoryboard parent element.  You can declare several Storyboard elements for different animations within a BeginStoryboard element.

As I stated in previous section, each Storyboard consists of a collection of animation type elements that specify the type and behavior of that animation.  A storyboard element has two important attributes:

·         Storyboard.Targetname: This is the target element name.

·         Storyboard.TargetProperty: The attribute that should be affected for the element that is specified in Storyboard.Targetname attribute.

On the basis of the attribute type you are going to animate, you need to choose one of the animation types.  For example, if you want to animate the width of an element, you should use DoubleAnimation.  If you want to animate the color of an attribute, you should use ColorAnimation.

There are two common types of animations: ColorAnimation and DoubleAnimation (there are other types which are similar to implement, so I will just give my examples with these two types).  You should choose one of the types based on your needs.

Regardless of the type of animation you are declaring, animations have five common attributes to set.

·         From: The value of the attribute at the start of animation.

·         To: The value of the attribute when animation ends.

·         By: The intermediate value of the attribute between the first and last values.

·         Duration: The duration of the animation.

·         RepeatBehavior: Specifies the behavior of animation.  It will be discussed in more detail later.

Let us see some examples.

Listing 1 is an animation that changes the width of a Button from 100 to 200 when the mouse enters the Button area.  It takes five seconds then ends.  It is very simple and does not need any comment.

Listing 1

<Window x:Class="AnimationsInXAML.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Animations In XAML" Height="180" Width="300"
    >
  <StackPanel>
    <StackPanel.Triggers>
      <EventTrigger RoutedEvent="Rectangle.MouseEnter">
        <EventTrigger.Actions>
          <BeginStoryboard>
            <Storyboard Storyboard.TargetName="btnSend" 
                        Storyboard.TargetProperty="Width">
              <DoubleAnimation From="100"
                               To="200" 
                               Duration="0:0:5"/>
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger.Actions>
      </EventTrigger>
    </StackPanel.Triggers>
    <Button Name="btnSend" 
            Width="100"
            Height="40"
            Margin="40"
            Background="Thistle">
      ASP Alliance
    </Button>
  </StackPanel>
</Window>

Some steps of the result are shown in Figure 1, Figure 2 and Figure 3.

Figure 1

Figure 2

Figure 3

Listing 2 is another example with better details.  It changes the background color of a Button from red to blue in five seconds, but uses "By attribute" to do this so it animates from red to beige then to blue smoothly.  Notice the usage of the new hierarchy address for Storyboard.TargetProperty value.

Listing 2

<Window x:Class="AnimationsInXAML.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Animations In XAML" Height="180" Width="300"
    >   
  <StackPanel>
    <StackPanel.Triggers>
      <EventTrigger RoutedEvent="Rectangle.MouseEnter">
        <EventTrigger.Actions>
          <BeginStoryboard>
            <Storyboard Storyboard.TargetName="btnSend" 
                        Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)">
              <ColorAnimation From="Red"
                              To="Blue"
                              By="Beige" 
                              Duration="0:0:5" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger.Actions>        
      </EventTrigger>
    </StackPanel.Triggers>
    <Button Name="btnSend" 
            Width="100"
            Height="40"
            Margin="40"
            Background="Red">
      ASP Alliance
    </Button>
  </StackPanel>
</Window>

Some steps of the result are shown in Figure 3, Figure 4 and Figure 5.

Figure 3

Figure 4

Figure 5

Listing 3 is a better example because it animates the width of a Button from 100 to 200, but this time with Forever as the RepeatBehavior.  This causes the animation to follow until the application stops.

Listing 3:

<Window x:Class="AnimationsInXAML.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Animations In XAML" Height="180" Width="300"
    >
  <StackPanel>
    <StackPanel.Triggers>
      <EventTrigger RoutedEvent="Rectangle.MouseEnter">
        <EventTrigger.Actions>
          <BeginStoryboard>
            <Storyboard Storyboard.TargetName="btnSend" 
                        Storyboard.TargetProperty="Width">
              <DoubleAnimation From="100"
                               To="200" 
                               Duration="0:0:5"
                               RepeatBehavior="Forever"/>
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger.Actions>
      </EventTrigger>
    </StackPanel.Triggers>
    <Button Name="btnSend" 
            Width="100"
            Height="40"
            Margin="40"
            Background="Plum">
      ASP Alliance
    </Button>
  </StackPanel>
</Window>

Results are shown in Figure 7, Figure 8 and Figure 9.  Note that this animation follows until you close the Window.

Figure 7

Figure 8

Figure 9

Change the Behavior of Animations

So far you have seen how the Forever value for RepeatBehavior of an animation causes it to run forever.  You can change this behavior with other values and also reverse your animations by setting their AutoReverse attribute to true.

In addition to Forever value for RepeatBehavior, you can specify the number of iterations.  Listing 4 is an animation that changes the width of a Button from 100 to 200 in five seconds, but twice (it is a modified version of Listing 3).  It also reverses itself. The result is shown in Figure 10, Figure 11 and Figure 12.

Listing 4

<Window x:Class="AnimationsInXAML.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Animations In XAML" Height="180" Width="300"
    >
  <StackPanel>
    <StackPanel.Triggers>
      <EventTrigger RoutedEvent="Rectangle.MouseEnter">
        <EventTrigger.Actions>
          <BeginStoryboard>
            <Storyboard Storyboard.TargetName="btnSend" 
                        Storyboard.TargetProperty="Width">
              <DoubleAnimation From="100"
                               To="200" 
                               Duration="0:0:5"
                               RepeatBehavior="2x"
                               AutoReverse="True"/>
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger.Actions>
      </EventTrigger>
    </StackPanel.Triggers>
    <Button Name="btnSend" 
            Width="100"
            Height="40"
            Margin="40"
            Background="Plum">
      ASP Alliance
    </Button>
  </StackPanel>
</Window>

Figure 10

Figure 11

Figure 12

Transformations

Not only you can animate your element by changing its attributes, but you can animate it with different types of transforms in several directions.  You can use a Storyboard and one type of animation to change the RenderTransform of an element on runtime.

Listing 5 shows an example of this.  It changed the Angle attribute of a RotateTransform which belongs to the Button on runtime from 0 to 90 degrees.  Take a look at some frames of the results in Figure 13, Figure 14 and Figure 15.

Listing 5

<Window x:Class="AnimationsInXAML.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Animations In XAML" Height="180" Width="300"
    >
  <StackPanel>
    <StackPanel.Triggers>
      <EventTrigger RoutedEvent="Rectangle.MouseEnter">
        <EventTrigger.Actions>
          <BeginStoryboard>
            <Storyboard Storyboard.TargetName="btnSend" 
                        Storyboard.TargetProperty="(RenderTransform).(RotateTransform.Angle)">
              <DoubleAnimation From="0"
                               To="90" 
                               Duration="0:0:5"/>
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger.Actions>
      </EventTrigger>
    </StackPanel.Triggers>
    <Button Name="btnSend" 
            Width="100"
            Height="40"
            Margin="40"
            Background="Plum">
      <Button.RenderTransform>
        <RotateTransform Angle="0" CenterX="40" CenterY="40" />
      </Button.RenderTransform>
      ASP Alliance
    </Button>
  </StackPanel>
</Window>

Figure 13

Figure 14

Figure 15

ParallelTimeline

Creating two animations that animate synchronously is as simple as declaring those animations and grouping them into a ParallelTimeline element.

Listing 6 and its results in Figure 16, Figure 17 and Figure 18 are an example for the ParallelTimeline element.  The Angle of RenderTransform of a Button is changing while its width is decreasing.

Listing 6

<Window x:Class="AnimationsInXAML.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Animations In XAML" Height="180" Width="300"
    >
  <StackPanel>
    <StackPanel.Triggers>
      <EventTrigger RoutedEvent="Rectangle.MouseEnter">
        <EventTrigger.Actions>
          <BeginStoryboard>             
            <Storyboard>
              <ParallelTimeline>
                <Storyboard Storyboard.TargetName="btnSend" 
                            Storyboard.TargetProperty="(RenderTransform).(RotateTransform.Angle)">
                  <DoubleAnimation From="0"
                                   To="90" 
                                   Duration="0:0:5"/>
                </Storyboard>
                <Storyboard Storyboard.TargetName="btnSend" 
                            Storyboard.TargetProperty="Width">
                  <DoubleAnimation From="100"
                                   To="70" 
                                   Duration="0:0:5"/>
                </Storyboard>
              </ParallelTimeline>
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger.Actions>
      </EventTrigger>
    </StackPanel.Triggers>
    <Button Name="btnSend" 
            Width="100"
            Height="40"
            Margin="40"
            Background="Plum">
      <Button.RenderTransform>
        <RotateTransform Angle="0" CenterX="40" CenterY="40" />
      </Button.RenderTransform>
      ASP Alliance
    </Button>
  </StackPanel>
</Window>

Figure 16

Figure 17

Figure 18

Key Frames

The last thing that you should know is about using Key Frames.  Suppose you want to declare an animation that moves from one frame to another, but not by default increments.  In this case you can declare all frames for your animation using one of KeyFrame elements.

Listing 7 is an example that shows everything you need.  It changes the width of a Button on the basis of custom frames that are declared using LinearDoubleKeyFrames which are declared in a DoubleAnimationUsingKeyFrames.

Listing 7

<Window x:Class="AnimationsInXAML.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Animations In XAML" Height="180" Width="300"
    >
  <StackPanel>
    <StackPanel.Triggers>
      <EventTrigger RoutedEvent="Rectangle.MouseEnter">
        <EventTrigger.Actions>
          <BeginStoryboard>             
                <Storyboard Storyboard.TargetName="btnSend" 
                            Storyboard.TargetProperty="Width">
                  <DoubleAnimationUsingKeyFrames Duration="0:0:5">
                    <LinearDoubleKeyFrame Value="100" KeyTime="0:0:1" />
                    <LinearDoubleKeyFrame Value="110" KeyTime="0:0:2" />
                    <LinearDoubleKeyFrame Value="120" KeyTime="0:0:3" />
                    <LinearDoubleKeyFrame Value="130" KeyTime="0:0:4" />
                    <LinearDoubleKeyFrame Value="140" KeyTime="0:0:5" />
                  </DoubleAnimationUsingKeyFrames>                    
                </Storyboard>
          </BeginStoryboard>
        </EventTrigger.Actions>
      </EventTrigger>
    </StackPanel.Triggers>
    <Button Name="btnSend" 
            Width="100"
            Height="40"
            Margin="40"
            Background="Aquamarine">
      ASP Alliance
    </Button>
  </StackPanel>
</Window>

Some frames of the results are shown in Figure 19, Figure 20 and Figure 21.

Figure 19

Figure 20

Figure 21

References

Summary

In this article I showed you the principles of animations in XAML and Windows Presentation Foundation.  After talking about Storyboard and fundamentals to declare an animation, I described some ways to control the behavior of animations.

After that I gave a short description about Transformations and ParallelTimeLines as a way to animate elements synchronously.

Finally, I finished my article by talking about Key Frame animations as a way to declare custom frames for an animation.



User Comments

Title: Well Done and Thank you   
Name: Shrishail V. Halijol
Date: 2009-04-07 2:26:26 PM
Comment:
Hi...
Thanks for such a small article which covered this basics of animation in Silverlight. I really enjoyed reading this article.
Thanks again for the good work. Well done.
Title: Phenomenal   
Name: John A. Blesson
Date: 2009-01-21 4:49:10 AM
Comment:
I just loved the tutorials . Wanting to have more of it.

Keepup the good work.
Title: Go on guy...   
Name: Nima
Date: 2008-01-18 3:34:07 PM
Comment:
Dear Keyvan,
I like the way you do it. Short, fast and complete enough to get start with. I'm proud of such Iranians.
Take care
Title: Useful Information   
Name: Soumya
Date: 2007-06-15 11:00:08 AM
Comment:
Could you please post information/Code about making flyouts in XAML. When we click a button we should be able to get a flyout and then when we click anywhere on the flyout, it should close.
Title: Thanks   
Name: Keyvan Nayyeri
Date: 2006-10-25 12:19:24 PM
Comment:
Thank you Mohammad,

I'll try to follow these turotials :-) I'm glad to see they could be useful.
Title: Very Nicely Done!!1   
Name: AzamSharp
Date: 2006-10-25 11:38:51 AM
Comment:
Hi,

Very nice tutorials. Keep up the good work :)
Title: Resources in XAML   
Name: Keyvan Nayyeri
Date: 2006-10-25 12:34:42 AM
Comment:
Third part of these tutorials about Resources in XAML:
http://aspalliance.com/1032_Resources_in_XAML
Title: Layout in XAML   
Name: Keyvan Nayyeri
Date: 2006-10-25 12:34:11 AM
Comment:
Second part of these tutorials about Layout in XAML:
http://aspalliance.com/1023_Layout_in_XAML
Title: Introduction to XAML   
Name: Keyvan Nayyeri
Date: 2006-10-25 12:33:50 AM
Comment:
First part of these tutorials, Introduction to XAML:
http://aspalliance.com/1019_Introduction_to_XAML

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 6:58:45 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search