Another interesting thing that you might think very interesting is the support of Animation for WPF. Basically, by the word Animation, we generally think of large Texture graphics in 3D space, which would probably be created in 3DS MAX studio or MAC etc. But believe me there is nothing to worry about this in case of WPF. WPF simplifies the concept Animation to be the change of a property over time.
Say for instance, say you want your textbox to change its color over time, you would write a simple color animation to do this or say you want to change the Opacity of a Border element during time, you need Double Animation to do this. Animation is cool if you are clear about how it works.
Type of Animation
I must say, don't make yourself more confused by seeing the types of Animation.
Animation is actually categorized in the same way as you categorize variables. Say for instance :
1. DoubleAnimation : This will animate a Double Value from one value to another. So if you want to change the Width of a TextBox over time you need DoubleAnimation.
2. ColorAnimation : Same as the above if the type of Changing element is Color, you need ColorAnimation.
3. SingleAnimation, RectAnimation, PointAnimation, Int32Animaition, ThicknessAnimation etc each of them bears the same meaning.
So basically the basis of Animation types is based on the type of the property for which you want your animation to work on.
Animation can also be categorized into two basic ways :
1. Animation Without KeyFrames : These are animation that only needs two values, From and To. It gives you a smooth animation based on the Timeline.DesiredFramerate property for the animation.
2. Animation With KeyFrames : Allows you to specify a KeyFrame collection which lets you define the KeyFrame value on a specified time. So that you can adjust your own animation based on specific time intervals.
Let us take a look at a few examples to make you understand animation feature of WPF:
<Window.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever">
<DoubleAnimation Storyboard.TargetProperty="Width"
From="300" To="200" AutoReverse="True" Duration="0:0:5" ></DoubleAnimation>
<DoubleAnimation Storyboard.TargetProperty="Height"
From="300" To="200" AutoReverse="True" Duration="0:0:5"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
In the above code, I have defined an EventTrigger which lets you have a DoubleAnimation(as Width is double value) on Width of the Window. We use Loaded
Event to start a StoryBoard.
What is a StoryBoard ?
A StoryBoard can be defined as a Container for TimeLines or a collection of animation timelines for which the object specified in Target will animate. We use StoryBoard to specify Animation within it.
Few important properties of StoryBoard :
=> RepeatBehaviour : Specifies the number of times for which the StoryBoard repeat the animation.
=> Target : Specifies the Target for which the storyboard will be applied to.
=> TargetName : Defines the target and reference it by its name attribute.
=> TargetProperty : Specifies the property for which the animation will be applied for.
=> AccelerationRatio / DecelerationRatio : Defines the acceleration or deceleration for the animation.
=> AutoReverse : Defines whether the StoryBoard will be reversed automatically.
This is really cool concept, which allows you to get the reverse of the storyboard timeline automatically generated by the WPF.
Animation can also be applied from code.
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 1.0;
myDoubleAnimation.To = 0.0;
myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));In the above code I have declared a DoubleAnimation which starts From 1.0 and moves to 0.0 in 5 seconds.
Animation with KeyFrames
Animation can be defined either using KeyFrames or without KeyFrames. KeyFrame allows you to define an intermediate frame so that the animation occurs for each individual frame intervals. There are three types of interpolation for an AnimationwithKeyFrames.
1. Linear
2. Discrete
3. Spline
Linear
Lets create an animation using KeyFrames :
<Border Background="Violet"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" >
<Border.Triggers>
<EventTrigger RoutedEvent="Border.MouseLeftButtonDown">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="transformObj"
Storyboard.TargetProperty="X"
Duration="0:0:15">
<LinearDoubleKeyFrame Value="500"
KeyTime="0:0:3" />
<LinearDoubleKeyFrame Value="50"
KeyTime="0:0:7" />
<LinearDoubleKeyFrame Value="300"
KeyTime="0:0:13" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Border.Triggers>
<Border.RenderTransform>
<TranslateTransform x:Name="transformObj" X="0" Y="0" />
</Border.RenderTransform>
</Border>
Here the animation is applied as LinearDoubleKeyFrame, which means the animation would be smooth while we define each KeyFrame value based on KeyTime. Here we change the Translation of the Border based on different KeyTime specified such that on 3rd second, the Rectangle will move to 500, at 7th second it will be at 50 and at 13th second it will be at 300. The animation is LinearDouble so the animation is smooth and steady.
Discrete
If I change the animation to DiscreteAnimation it will place the object only at the KeyTime specified
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="transformObj"
Storyboard.TargetProperty="X"
Duration="0:0:15">
<DiscreteDoubleKeyFrame Value="500" KeyTime="0:0:3" />
<DiscreteDoubleKeyFrame Value="50" KeyTime="0:0:7" />
<DiscreteDoubleKeyFrame Value="300" KeyTime="0:0:13" />
</DoubleAnimationUsingKeyFrames>
Thus changing the LinearDouble with DiscreteDouble makes it change its position all of a sudden based on the KeyTime specified for the animation.
Spline
Spline Animation is used to define more realistic animation behavior for your control. It lets you control acceleration and deceleration of the animation. With KeySpline you can define the the cubic bazier curve using Spline Key frame.
Lets look at the example
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="transformObj"
Storyboard.TargetProperty="X"
Duration="0:0:15">
<SplineDoubleKeyFrame Value="500" KeyTime="0:0:3" KeySpline="0.0,0.1
0.1,0.1" />
<SplineDoubleKeyFrame Value="50" KeyTime="0:0:7" KeySpline="0.0,0.1
0.1,0.1"/>
<SplineDoubleKeyFrame Value="300" KeyTime="0:0:13" KeySpline="0.0,0.1
0.1,0.1"/>
</DoubleAnimationUsingKeyFrames>
Thus you can see KeySpline allows you to define a smooth animation that starts with acceleration and with highest speed in the middle and ultimately decelerates back again. You can also use Easing Function to specify the custom formulas for the animation.
Say for instance, say you want your textbox to change its color over time, you would write a simple color animation to do this or say you want to change the Opacity of a Border element during time, you need Double Animation to do this. Animation is cool if you are clear about how it works.
Type of Animation
I must say, don't make yourself more confused by seeing the types of Animation.
Animation is actually categorized in the same way as you categorize variables. Say for instance :
1. DoubleAnimation : This will animate a Double Value from one value to another. So if you want to change the Width of a TextBox over time you need DoubleAnimation.
2. ColorAnimation : Same as the above if the type of Changing element is Color, you need ColorAnimation.
3. SingleAnimation, RectAnimation, PointAnimation, Int32Animaition, ThicknessAnimation etc each of them bears the same meaning.
So basically the basis of Animation types is based on the type of the property for which you want your animation to work on.
Animation can also be categorized into two basic ways :
1. Animation Without KeyFrames : These are animation that only needs two values, From and To. It gives you a smooth animation based on the Timeline.DesiredFramerate property for the animation.
2. Animation With KeyFrames : Allows you to specify a KeyFrame collection which lets you define the KeyFrame value on a specified time. So that you can adjust your own animation based on specific time intervals.
Let us take a look at a few examples to make you understand animation feature of WPF:
<Window.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever">
<DoubleAnimation Storyboard.TargetProperty="Width"
From="300" To="200" AutoReverse="True" Duration="0:0:5" ></DoubleAnimation>
<DoubleAnimation Storyboard.TargetProperty="Height"
From="300" To="200" AutoReverse="True" Duration="0:0:5"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
In the above code, I have defined an EventTrigger which lets you have a DoubleAnimation(as Width is double value) on Width of the Window. We use Loaded
Event to start a StoryBoard.
What is a StoryBoard ?
A StoryBoard can be defined as a Container for TimeLines or a collection of animation timelines for which the object specified in Target will animate. We use StoryBoard to specify Animation within it.
Few important properties of StoryBoard :
=> RepeatBehaviour : Specifies the number of times for which the StoryBoard repeat the animation.
=> Target : Specifies the Target for which the storyboard will be applied to.
=> TargetName : Defines the target and reference it by its name attribute.
=> TargetProperty : Specifies the property for which the animation will be applied for.
=> AccelerationRatio / DecelerationRatio : Defines the acceleration or deceleration for the animation.
=> AutoReverse : Defines whether the StoryBoard will be reversed automatically.
This is really cool concept, which allows you to get the reverse of the storyboard timeline automatically generated by the WPF.
Animation can also be applied from code.
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 1.0;
myDoubleAnimation.To = 0.0;
myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));In the above code I have declared a DoubleAnimation which starts From 1.0 and moves to 0.0 in 5 seconds.
Animation with KeyFrames
Animation can be defined either using KeyFrames or without KeyFrames. KeyFrame allows you to define an intermediate frame so that the animation occurs for each individual frame intervals. There are three types of interpolation for an AnimationwithKeyFrames.
1. Linear
2. Discrete
3. Spline
Linear
Lets create an animation using KeyFrames :
<Border Background="Violet"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" >
<Border.Triggers>
<EventTrigger RoutedEvent="Border.MouseLeftButtonDown">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="transformObj"
Storyboard.TargetProperty="X"
Duration="0:0:15">
<LinearDoubleKeyFrame Value="500"
KeyTime="0:0:3" />
<LinearDoubleKeyFrame Value="50"
KeyTime="0:0:7" />
<LinearDoubleKeyFrame Value="300"
KeyTime="0:0:13" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Border.Triggers>
<Border.RenderTransform>
<TranslateTransform x:Name="transformObj" X="0" Y="0" />
</Border.RenderTransform>
</Border>
Here the animation is applied as LinearDoubleKeyFrame, which means the animation would be smooth while we define each KeyFrame value based on KeyTime. Here we change the Translation of the Border based on different KeyTime specified such that on 3rd second, the Rectangle will move to 500, at 7th second it will be at 50 and at 13th second it will be at 300. The animation is LinearDouble so the animation is smooth and steady.
Discrete
If I change the animation to DiscreteAnimation it will place the object only at the KeyTime specified
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="transformObj"
Storyboard.TargetProperty="X"
Duration="0:0:15">
<DiscreteDoubleKeyFrame Value="500" KeyTime="0:0:3" />
<DiscreteDoubleKeyFrame Value="50" KeyTime="0:0:7" />
<DiscreteDoubleKeyFrame Value="300" KeyTime="0:0:13" />
</DoubleAnimationUsingKeyFrames>
Thus changing the LinearDouble with DiscreteDouble makes it change its position all of a sudden based on the KeyTime specified for the animation.
Spline
Spline Animation is used to define more realistic animation behavior for your control. It lets you control acceleration and deceleration of the animation. With KeySpline you can define the the cubic bazier curve using Spline Key frame.
Lets look at the example
<DoubleAnimationUsingKeyFrames
Storyboard.TargetName="transformObj"
Storyboard.TargetProperty="X"
Duration="0:0:15">
<SplineDoubleKeyFrame Value="500" KeyTime="0:0:3" KeySpline="0.0,0.1
0.1,0.1" />
<SplineDoubleKeyFrame Value="50" KeyTime="0:0:7" KeySpline="0.0,0.1
0.1,0.1"/>
<SplineDoubleKeyFrame Value="300" KeyTime="0:0:13" KeySpline="0.0,0.1
0.1,0.1"/>
</DoubleAnimationUsingKeyFrames>
Thus you can see KeySpline allows you to define a smooth animation that starts with acceleration and with highest speed in the middle and ultimately decelerates back again. You can also use Easing Function to specify the custom formulas for the animation.
No comments:
Post a Comment