動畫和計時系統概觀Animation and Timing System Overview

本主題介紹計時系統如何使用動畫TimelineClock類為屬性設置動畫。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時間段的方式,動畫是生成輸出值的類型。 TimelineThe Animation Overview described how a Timeline represents a segment of time, and an animation is a type of Timeline that produces output values. 就其本身,a 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是一種特殊類型的物件,它維護 的Timeline計時相關運行時狀態。A Clock is a special type of object that maintains timing-related run-time state for the Timeline. 它提供了對動畫和計時系統至關重要的三位資訊:CurrentTimeCurrentProgressCurrentStateIt provides three bits of information that are essential to the animation and timing system: CurrentTime, CurrentProgress, and CurrentState. AClockTimeline使用其Duration: 、 RepeatBehavior AutoReverse、 、 等描述的計時行為確定其目前時間、進度和狀態。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. 還可以使用ClockCreateClock的方法顯式創建TimelineYou can also create a Clock explicitly by using the CreateClock method of your Timeline. 該方法MediaTimeline.CreateClockTimeline調用它的的創建適當類型的時鐘。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. 下表顯示了對應于某些不同類型的ClockTimeline類型。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創建的任何物件應用於相容的依賴項屬性ApplyAnimationClockYou 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. 提供AnimationClockAnimationTimeline當前本地時間 、輸入值(通常是屬性的基值)和預設目標值。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 訪問器按屬性檢索動畫的值時,您將獲得其AnimationClock的輸出。When 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. 下圖顯示了時間管理器ClockGroup、、和AnimationClock動畫依賴項屬性之間的關係。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. 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屬性,請使用 應用 的 動畫 或AnimationClock時使用 。To 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切換狀態並在刻度之間返回到其原始狀態(例如從Active到 和Stopped返回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器或 方法檢索屬性的值將返回AnimationClockAnimationClockActive時 的輸出。 FillingRetrieving 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