Tải bản đầy đủ - 0 (trang)
17-29. Animate the Property of a Control

17-29. Animate the Property of a Control

Tải bản đầy đủ - 0trang

CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION



Table 17-12. Commonly Used Properties of the Timeline Class



Property



Description



AccelerationRatio



Used to specify a percentage of the timeline’s duration that should be used to

accelerate the speed of the animation from 0 to the animation’s maximum rate.

The value should be a System.Double ranging between 0 and 1, inclusive, and is 0

by default. The sum of a timeline’s AccelerationRatio and DecelerationRatio must

not be greater than 1.



AutoReverse



A System.Boolean property that specifies whether the Timeline should play back to

the beginning once the end has been reached.



BeginTime



A System.Nullable(TimeSpan) that specifies when a timeline should become active,

relative to its parent’s BeginTime. For a root Timeline, the offset is taken from the

time that it becomes active. This value can be negative and will start the Timeline

from the specified offset, giving the appearance that the Timeline has already been

playing for the given time. The SpeedRatio of a Timeline has no effect on its

BeginTime value, although it is affected by its parent SpeedRatio. If the property is

set to null, the Timeline will never begin.



DecelerationRatio



Used to specify a percentage of the timeline’s duration that should be used to

reduce the speed of the animation from the maximum rate to 0. The value should

be a System.Double ranging between 0 and 1, inclusive, and is 0 by default. The

sum of a timeline’s AccelerationRatio and DecelerationRatio must not be greater

than 1.



Duration



A nullable System.Windows.Duration specifying the length of time the animation

should take to play from beginning to end. For Storyboard and ParallelTimeline

objects, this value will default to the longest duration of its children. For a basic

AnimationTimeline object—for example,

System.Windows.Media.Animation.DoubleAnimation—this value will default to 1

second, and a keyframe-based animation will have a value equal to the sum of

System.Windows.Media.Animation.KeyTime values for each keyframe.



FillBehavior



A value of the System.Windows.Media.Animation.FillBehavior enumeration is used

to define an animation’s behavior once it has completed, but its parent is still

active, or its parent is in its hold period. The FillBehavior.HoldEnd value is used

when an animation should hold its final value for a property until its parent is no

longer active or outside of its hold period. The FillBehavior.Stop value will cause

the timeline to not hold its final value for a property once it completes, regardless

of whether its parent is still active.



887



www.it-ebooks.info



CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION



Property



Description



RepeatBehavior



A System.Windows.Media.Animation.RepeatBehavior value indicating whether and

how an animation is repeated.



SpeedRatio



A property of type System.Double that is used as a multiplier to alter the playback

speed of an animation. A speed ratio of 0.25 will slow the animation down such

that it runs at a quarter of its normal speed. A value of 2 will double the speed of

the animation, and a speed ratio of 1 means the animation will play back at normal

speed. Note that this will affect the actual duration of an animation.



The Code

The following example demonstrates some of the functionality available with animations. Properties of

various controls are animated using different values for the previously discussed properties to give an

example of their effect.


xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Recipe17_29" Height="300" Width="300">




Storyboard.TargetName="ellipse1">




To="50"

Duration="0:0:5"

AccelerationRatio="0.25"

DecelerationRatio="0.25"

Storyboard.TargetProperty="Width"

RepeatBehavior="5x" />


To="50"

Duration="0:0:5"

AccelerationRatio="0.5"

DecelerationRatio="0.25"

Storyboard.TargetProperty="Height"

RepeatBehavior="5x"

SpeedRatio="4" />






Storyboard.TargetName="rect1">




To="50"

Duration="0:0:10"



888



www.it-ebooks.info



CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION



FillBehavior="Stop"

Storyboard.TargetProperty="Width" />


To="50"

Duration="0:0:5"

FillBehavior="HoldEnd"

AccelerationRatio="0.5"

DecelerationRatio="0.25"

Storyboard.TargetProperty="Height" />










RoutedEvent="Ellipse.Loaded"

SourceName="ellipse1">


Storyboard="{DynamicResource ellipse1Storyboard}" />




RoutedEvent="Rectangle.Loaded"

SourceName="rect1">


Storyboard="{StaticResource rect1Storyboard}" />
















Fill="CornflowerBlue" />


Fill="Firebrick" Grid.Column="1" />







17-30. Animate Several Properties in Parallel

Problem

You need to animate several properties of a control at the same time—for example, its height, width, and

color.



889



www.it-ebooks.info



CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION



Solution

Define your animations as discussed in Recipe 17-29, but make them children of a

System.Windows.Media.Animation.ParallelTimeline.



How It Works

The ParallelTimeline is a special type of System.Windows.Media.Animation.Timeline that allows for one

or more child Timeline objects to be defined as its children, with each child Timeline being run in

parallel. Because ParallelTimeline is a Timeline object, it can be used like any other Timeline object.

Unlike a Storyboard, where animations are activated based on the order in which its child Timeline

objects are declared, a ParallelTimeline will activate its children based on the value of their BeginTime

properties. If any of the animations overlap, they will run in parallel.

The Storyboard class actually inherits from ParallelTimeline, and simply gives each child a

BeginTime based on where in the list of child objects a Timeline is declared and the cumulative Duration

and BeginTime values of each preceding Timeline. The Storyboard class goes further to extend the

ParallelTimeline class by adding a number of methods for controlling the processing of its child

Timeline objects. Because ParallelTimeline is the ancestor of a Storyboard, ParallelTimeline objects

are more suited to nesting because they are much slimmer objects.

Like other Timeline objects, the ParallelTimeline has a BeginTime property. This allows you to

specify an offset from the start of the owning Storyboard to the activation of the ParallelTimeline. As a

result, if a value for BeginTime is given by the ParallelTimeline, its children’s BeginTime will work relative

to this value, as opposed to being relative to the Storyboard.

It is important to note that a Storyboard.Completed event will not be raised on the owning

Storyboard until the last child Timeline in the ParallelTimeline finishes. This is because a

ParallelTimeline can contain Timeline objects with different BeginTime and Duration values, meaning

they won’t all necessarily finish at the same time.



The Code

The following example defines a System.Windows.Window that contains a single

System.Windows.Shapes.Rectangle. When the mouse is placed over the rectangle, the Rectangle.Height,

Rectangle.Width, and Rectangle.Fill properties are animated. The animation continues until the

mouse is moved out of the rectangle.


xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Recipe17_30" Height="300" Width="300">




Stroke="Black" StrokeThickness="1">