Compartir a través de


Cómo: Establecer una duración para una animación

La Timeline representa un segmento de tiempo y la longitud de ese segmento viene determinada por la línea temporal. Cuando un Timeline llega al final de su duración, deja de reproducirse. Si tiene Timeline escalas de tiempo secundarias, también dejan de jugar. En el caso de una animación, Duration especifica cuánto tiempo tarda la animación en pasar de su valor inicial a su valor final.

Puede especificar un Duration con un tiempo finito y específico o con los valores especiales Automatic o Forever. La duración de una animación siempre debe ser un valor de tiempo, ya que una animación siempre debe tener una longitud definida y finita; de lo contrario, la animación no sabría cómo realizar la transición entre sus valores de destino. Las líneas de tiempo de contenedores (TimelineGroup objetos), como Storyboard y ParallelTimeline, tienen una duración predeterminada de Automatic, lo que significa que finalizan automáticamente cuando su último elemento secundario deja de reproducirse.

En el siguiente ejemplo, se anima el ancho, el alto y el color de relleno de un Rectangle. Las duraciones se establecen en las líneas de tiempo de animación y del contenedor, lo que da lugar a efectos de animación, incluyendo el control de la velocidad percibida de una animación y la anulación de la duración de las líneas de tiempo hijas con la duración de una línea de tiempo del contenedor.

Ejemplo

<Page 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <StackPanel Margin="20">
  
    <Rectangle Width="100" Height="100" Name="myRectangle">
      <Rectangle.Fill>
        <SolidColorBrush x:Name="MyAnimatedBrush" Color="Black" />
      </Rectangle.Fill>
      <Rectangle.Triggers>
      
        <!-- Animates the rectangle fill to yellow and width to 300. -->
        <EventTrigger RoutedEvent="Rectangle.Loaded">
          <BeginStoryboard>

            <!-- By default, TimelineGroup objects like Storyboard and ParallelTimeline have 
            a Duration of "Automatic". A TimelineGroup's automatic duration encompasses its 
            last-ending child. In this example, there is only one child of the Storyboard, the
            ParallelTimeline, so when the ParallelTimeline ends, the Storyboard duration will
            automatically end. -->
            <Storyboard>

              <!-- This ParallelTimeline has overriden its default duration of "Automatic" with
              a finite duration of half a second. This will force this Timeline to end after half a
              second even though its child Timelines have a longer duration (2 and 4 seconds respectively). 
              This cuts off the animation prematurely and the rectangle's fill will not go all the way to 
              yellow nor will the rectangle width get all the way to 300. Again, the default duration of a
              ParallelTimeline is "Automatic" so if you remove the finite duration, the ParallelTimeline
              will wait for its child timelines to end before it ends. -->

              <!-- Note: To specify a finite time in XAML, use the syntax of "days:hours:seconds". As mentioned,
              this ParallelTimeline has a duration of half a second. -->
              <ParallelTimeline Duration="0:0:0.5">

                <!-- For Animation Timelines like DoubleAnimation, the duration is one factor that
                determines the rate at which an animation appears to progress. For example, the DoubleAnimation
                below that animates the rectangle height will complete in only one second while the animation
                that animates the width willwill complete in 2 seconds which is relatively fast compared to the DoubleAnimation
                which animates the rectangle width over 4 seconds. -->
                <DoubleAnimation
                  Storyboard.TargetName="myRectangle"
                  Storyboard.TargetProperty="Height"
                  To="300" Duration="0:0:1" />

                <DoubleAnimation
                  Storyboard.TargetName="myRectangle"
                  Storyboard.TargetProperty="Width"
                  To="300" Duration="0:0:4" />

                <ColorAnimation
                  Storyboard.TargetName="MyAnimatedBrush"
                  Storyboard.TargetProperty="Color"
                  To="Yellow" Duration="0:0:2" />

              </ParallelTimeline>
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
        
      </Rectangle.Triggers>
    </Rectangle>
  </StackPanel>
</Page>

Consulte también

  • Duration
  • Visión general de animación