动画和计时系统概述Animation and Timing System Overview

本主题描述计时系统如何使用动画Timeline,和Clock类属性进行动画处理。This topic describes how the timing system uses the animation, Timeline, and Clock classes to animate properties.

系统必备Prerequisites

为了了解本主题,应该能够使用 WPFWPF 动画来对属性进行动画处理,如动画概述中所述。To understand this topic, you should be able to use WPFWPF animations to animate properties, as described in the Animation Overview. 本主题还有助于熟悉依赖属性;有关详细信息,请参阅依赖属性概述It also helps to be familiar with dependency properties; for more information, see the Dependency Properties Overview.

时间线和时钟Timelines and Clocks

动画概述所述方式Timeline表示的时间和动画段是一种Timeline产生输出值。The Animation Overview described how a Timeline represents a segment of time, and an animation is a type of Timeline that produces output values. 其本身而言, Timeline,不执行任何操作而不只是介绍了一个时间段。By itself, a Timeline, doesn't do anything other than just describe a segment of time. 时间线的Clock对象执行实际工作。It's the timeline's Clock object that does the real work. 同样,动画并不实际属性进行动画处理: 动画类描述应如何计算输出值,但它是Clock创建动画促进动画输出并将其应用到属性。Likewise, animation doesn't actually animate properties: an animation class describes how output values should be calculated, but it’s the Clock that was created for the animation that drives the animation output and applies it to properties.

一个Clock是一种特殊的维护与时间有关运行时的状态对象TimelineA Clock is a special type of object that maintains timing-related run-time state for the Timeline. 它提供对动画和计时系统至关重要的三个信息位: CurrentTimeCurrentProgress,和CurrentStateIt provides three bits of information that are essential to the animation and timing system: CurrentTime, CurrentProgress, and CurrentState. 一个Clock通过使用所描述的计时行为来确定其当前时间、 进度和状态及其Timeline: DurationRepeatBehaviorAutoReverse,依次类推。A Clock determines its current time, progress, and state by using the timing behaviors described by its Timeline: Duration, RepeatBehavior, AutoReverse, and so on.

在大多数情况下,Clock会自动为你的时间线创建。In most cases, a Clock is created automatically for your timeline. 当您使用进行动画处理StoryboardBeginAnimation方法中,自动为时间线和动画创建时钟并应用于其目标属性。When you animate by using a Storyboard or the BeginAnimation method, clocks are automatically created for your timelines and animations and applied to their targeted properties. 此外可以创建Clock通过使用显式CreateClock方法在TimelineYou can also create a Clock explicitly by using the CreateClock method of your Timeline. MediaTimeline.CreateClock方法创建的相应类型的时钟Timeline上调用它。The MediaTimeline.CreateClock method creates a clock of the appropriate type for the Timeline on which it is called. 如果Timeline包含子时间线,它会创建Clock也为其对象。If the Timeline contains child timelines, it creates Clock objects for them as well. 得到Clock在匹配的结构的树中排列对象Timeline从中创建树对象。The resulting Clock objects are arranged in trees that match the structure of the Timeline objects tree from which they are created.

不同类型的时间线具有不同类型的时钟。There are different types of clocks for different types of timelines. 下表显示Clock对应于一些不同的类型Timeline类型。The following table shows the Clock types that correspond to some of the different Timeline types.

时间线类型Timeline type 时钟类型Clock type 时钟用途Clock purpose
动画 (继承自AnimationTimeline)Animation (inherits from AnimationTimeline) AnimationClock 为依赖属性生成输出值。Generates output values for a dependency property.
MediaTimeline MediaClock 处理媒体文件。Processes a media file.
ParallelTimeline ClockGroup 分组和控制其子Clock对象Groups and controls its child Clock objects
Storyboard ClockGroup 分组和控制其子Clock对象Groups and controls its child Clock objects

您可以应用任意AnimationClock对象通过创建到兼容的依赖属性ApplyAnimationClock方法。You can apply any AnimationClock objects you create to compatible dependency properties by using the ApplyAnimationClock method.

需要进行大量性能的情况下,例如对大量类似对象进行动画处理管理你自己Clock使用可以提供性能优势。In performance-intensive scenarios, such as animating large numbers of similar objects, managing your own Clock use can provide performance benefits.

时钟和时间管理器Clocks and the Time Manager

当您设置中的对象有动画效果WPFWPF,它是时间管理器,用于管理Clock为时间线创建的对象。When you animate objects in WPFWPF, it’s the time manager that manages the Clock objects created for your timelines. 时间管理器是 Clock 对象树的根,并控制该树中的时间流。The time manager is the root of a tree of Clock objects and controls the flow of time in that tree. 将自动为每个 WPFWPF 应用程序创建时间管理器,它对于应用程序开发人员不可见。A time manager is automatically created for each WPFWPF application and is invisible to the application developer. 时间管理器每秒钟“滴答”多次;每秒发生的实际滴答次数取决于可用的系统资源。The time manager "ticks" many times per second; the actual number of ticks that occur each second varies depending on available system resources. 在每个滴答,时间管理器计算的所有状态ActiveClock计时树中的对象。During each one of these ticks, the time manager computes the state of all Active Clock objects in the timing tree.

下图演示时间管理器之间的关系和AnimationClock,和动画处理的依赖属性。The following illustration shows the relationship between the time manager, and AnimationClock, and an animated dependency property.

计时系统组件Timing system components
对属性进行动画处理Animating a property

当时间管理器滴答时,它会更新的时间每个ActiveClock应用程序中。When the time manager ticks, it updates the time of every Active Clock in the application. 如果ClockAnimationClock,它使用GetCurrentValue方法的AnimationTimeline于创建它来计算其当前输出值。If the Clock is an AnimationClock, it uses the GetCurrentValue method of the AnimationTimeline from which it was created to calculate its current output value. AnimationClock提供AnimationTimeline当前本地时间、 输入的值,这通常是属性的基值,与默认目标值。The AnimationClock supplies the AnimationTimeline with the current local time, an input value, which is typically the base value of the property, and a default destination value. 检索一个经过动画处理的值时通过属性使用GetValue方法或其 CLR 访问器,你获得的输出其AnimationClockWhen you retrieve the value of an animated by property using the GetValue method or its CLR accessor, you get the output of its AnimationClock.

时钟组Clock Groups

前面部分介绍了如何有不同类型的Clock时间线的不同类型的对象。The preceding section described how there are different types of Clock objects for different types of timelines. 下图演示时间管理器之间的关系ClockGroupAnimationClock,和动画处理的依赖属性。The following illustration shows the relationship between the time manager, a ClockGroup, an AnimationClock, and an animated dependency property. 一个ClockGroup为进行分组的其他时间线,如创建Storyboard类,该类对动画和其他时间线。A ClockGroup is created for timelines that group other timelines, such as the Storyboard class, which groups animations and other timelines.

计时系统组件Timing system components
ClockGroupA ClockGroup

撰写Composition

可以将多个时钟与一个属性相关联,在这种情况下,每个时钟都将上一个时钟的输出值用作其基值。It's possible to associate multiple clocks with a single property, in which case each clock uses the output value of the preceding clock as its base value. 下图显示了三个AnimationClock应用于相同的属性对象。The following illustration shows three AnimationClock objects applied to the same property. 时钟1 将经过动画处理的属性的基值用作其输入,并使用该值生成输出。Clock1 uses the base value of the animated property as its input and uses it to generate output. 时钟2 将时钟1 的输出用作其输入,并使用该值生成输出。Clock2 takes the output from Clock1 as its input and uses it to generate output. 时钟3 将时钟2 的输出用作其输入,并使用该值生成输出。Clock3 takes the output from Clock2 as its input and uses it to generate output. 如果多个时钟同时影响同一个属性,则认为这些时钟位于一个组合链中。When multiple clocks affect the same property simultaneously, they are said to be in a composition chain.

计时系统组件Timing system components
组合链A composition chain

请注意,虽然在输入和输出之间创建关系AnimationClock中组合链中,其计时行为不会受到影响; 的对象Clock对象 (包括AnimationClock对象) 上其父级具有分层依赖性Clock对象。Note that although a relationship is created among the input and output of the AnimationClock objects in the composition chain, their timing behaviors are not affected; Clock objects (including AnimationClock objects) have a hierarchical dependency on their parent Clock objects.

若要将多个时钟应用于同一个属性,请使用ComposeHandoffBehavior应用时Storyboard,动画,或AnimationClockTo apply multiple clocks to the same property, use the Compose HandoffBehavior when applying a Storyboard, animation, or AnimationClock.

滴答和事件合并Ticks and Event Consolidation

除了计算输出值以外,时间管理器还会在它每滴答一次时执行其他工作:它会确定每个时钟的状态并根据需要引发事件。In addition to calculating output values, the time manager does other work every time it ticks: it determines the state of each clock and raises events as appropriate.

尽管时钟周期频繁发生,但是不同时钟周期之间可能会很多操作。While ticks occur frequently, it's possible for a lot of things to happen between ticks. 例如,Clock可能会停止、 启动,并再次停止,在这种情况下其CurrentState值将更改三次。For example, a Clock might be stopped, started, and stopped again, in which case its CurrentState value will have changed three times. 从理论上讲,CurrentStateInvalidated事件可能会引发多个时间在一个时钟周期中; 但是,计时引擎会合并事件,以便CurrentStateInvalidated每个滴答一次引发事件。In theory, the CurrentStateInvalidated event could be raised multiple times in a single tick; however, the timing engine consolidates events, so that the CurrentStateInvalidated event can be raised at most once per tick. 这适用于所有的计时事件: 针对引发一个事件最多每种类型的给定Clock对象。This is true for all timing events: at most one event of each type is raised for a given Clock object.

Clock切换状态并返回到其原始状态计时周期之间 (例如从ActiveStopped并返回到Active),仍发生关联的事件。When a Clock switches states and returns back to its original state between ticks (such as changing from Active to Stopped and back to Active), the associated event still occurs.

有关计时事件的详细信息,请参阅计时事件概述For more information about timing events, see the Timing Events Overview.

属性的当前值和基值Current Values and Base Values of Properties

可进行动画处理的属性具有两个值:基值和当前值。An animatable property can have two values: a base value and a current value. 在将使用其 CLR 访问器的属性的设置或SetValue方法,您并将其基值。When you set property using its CLR accessor or the SetValue method, you set its base value. 对于尚未进行动画处理的属性,基值和当前值相同。When a property is not animated, its base and current values are the same.

当您对属性进行动画处理AnimationClock设置的属性当前值。When you animate a property, the AnimationClock sets the property's current value. 检索通过其 CLR 访问器的属性的值或GetValue方法返回的输出AnimationClockAnimationClockActiveFillingRetrieving the property's value through its CLR accessor or the GetValue method returns the output of the AnimationClock when the AnimationClock is Active or Filling. 可以使用来检索属性的基值GetAnimationBaseValue方法。You can retrieve the property's base value by using the GetAnimationBaseValue method.

请参阅See also