Visão Geral de Animação e Sistema de Tempo
This topic describes how the timing system uses the animation, Timeline, and Clock classes to animate properties.
Prerequisites
To understand this topic, you should be able to use WPF animations to animate properties, as described in the Revisão de Animação. It also helps to be familiar with dependency properties; for more information, see the Visão geral sobre propriedades de dependência.
Timelines and Clocks
The Revisão de Animação described how a Timeline represents a segment of time, and an animation is a type of Timeline that produces output values. By itself, a Timeline, doesn't do anything other than just describe a segment of time. It's the timeline's Clock object that does the real work. Da mesma forma, a animação não realmente animar propriedades: uma classe de animação descreve como os valores de saída devem ser calculados, mas é a Clock que foi criado para a animação que a saída de animação de drives e o aplica a propriedades.
A Clock is a special type of object that maintains timing-related run-time state for the Timeline. Ele fornece três bits de informações que são essenciais para o sistema de temporização e de animação: CurrentTime, CurrentProgress, and CurrentState. A Clock determina a hora atual, o andamento e o estado usando os comportamentos de temporização descritos por seus Timeline: Duration, RepeatBehavior, AutoReverse, and so on.
In most cases, a Clock is created automatically for your timeline. 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. You can also create a Clock explicitly by using the CreateClock method of your Timeline. The MediaTimeline.CreateClock method creates a clock of the appropriate type for the Timeline on which it is called. If the Timeline contains child timelines, it creates Clock objects for them as well. 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. The following table shows the Clock types that correspond to some of the different Timeline types.
Timeline type |
Clock type |
Clock purpose |
---|---|---|
Animation (inherits from AnimationTimeline) |
Generates output values for a dependency property. |
|
Processes a media file. |
||
Groups and controls its child Clock objects |
||
Groups and controls its child Clock objects |
You can apply any AnimationClock objects you create to compatible dependency properties by using the ApplyAnimationClock method.
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
When you animate objects in WPF, it’s the time manager that manages the Clock objects created for your timelines. The time manager is the root of a tree of Clock objects and controls the flow of time in that tree. Um gerente de tempo é criado automaticamente para cada WPF aplicativo e é invisível para o desenvolvedor do aplicativo. O Gerenciador de tempo "ticks" muitas vezes por segundo; o número real de pulsos que ocorrem a cada segundo varia dependendo dos recursos disponíveis no sistema. During each one of these ticks, the time manager computes the state of all Active Clock objects in the timing tree.
The following illustration shows the relationship between the time manager, and AnimationClock, and an animated dependency property.
Animating a property
When the time manager ticks, it updates the time of every Active Clock in the application. 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. 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. 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
The preceding section described how there are different types of Clock objects for different types of timelines. The following illustration shows the relationship between the time manager, a ClockGroup, an AnimationClock, and an animated dependency property. A ClockGroup is created for timelines that group other timelines, such as the Storyboard class, which groups animations and other timelines.
A 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. The following illustration shows three AnimationClock objects applied to the same property. Clock1 uses the base value of the animated property as its input and uses it to generate output. Clock2 takes the output from Clock1 as its input and uses it to generate output. 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.
A composition chain
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.
To apply multiple clocks to the same property, use the Compose HandoffBehavior when applying a Storyboard, animation, or AnimationClock.
Ticks and Event Consolidation
Para além de calcular os valores de saída, o Gerenciador de tempo funciona outros toda vez que ele tiques: Ele determina o estado de cada relógio e gera eventos conforme apropriado.
While ticks occur frequently, it's possible for a lot of things to happen between ticks. For example, a Clock might be stopped, started, and stopped again, in which case its CurrentState value will have changed three times. 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. Isto é verdadeiro para todos os eventos de tempo: no máximo, um evento de cada tipo é gerado para um determinado Clock objeto.
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 Visão geral Eventos de Temporizadores.
Current Values and Base Values of Properties
Uma propriedade pode ser animada pode ter dois valores: um valor de base e o valor atual. 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.
When you animate a property, the AnimationClock sets the property's current value. Retrieving 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. You can retrieve the property's base value by using the GetAnimationBaseValue method.