動畫和計時系統概觀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.

AClock是一種特殊類型的物件,維護的計時相關執行階段狀態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. AClock判斷其目前的時間、 進度和狀態使用所描述的計時行為及其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. AClockGroup分組其他時間軸,例如建立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. Clock1 使用動畫屬性的基底值做為輸入,並使用它來產生輸出。Clock1 uses the base value of the animated property as its input and uses it to generate output. Clock2 會採用從 Clock1 的輸出做為輸入,並使用它來產生輸出。Clock2 takes the output from Clock1 as its input and uses it to generate output. Clock3 會採用從 Clock2 的輸出做為輸入,並使用它來產生輸出。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