如何:使用关键帧对大小变化进行动画处理

此示例演示如何使用关键帧对大小变化进行动画处理。

示例

下面的示例使用 SizeAnimationUsingKeyFrames 类对 ArcSegmentSize 属性进行动画处理。 此动画按以下方式使用三个关键帧:

  1. 在动画的前半秒中,使用 LinearSizeKeyFrame 类的实例逐渐增加弧的大小。LinearSizeKeyFrame 等线性关键帧在值之间创建一个平滑的线性过渡。

  2. 在后半秒结束时,使用 DiscreteSizeKeyFrame 类的实例突然增加弧的大小。离散关键帧(如 DiscreteSizeKeyFrame)在值之间创建突然暴增,即大小变化突然发生,而不是缓慢变化。

  3. 在最后两秒内,使用 SplineSizeKeyFrame 类的实例来增加弧的大小。自由绘制曲线关键帧(如 SplineSizeKeyFrame)将会根据 KeySpline 属性的值在值之间创建可变过渡。 在本例中,弧的大小开始时缓慢增加,然后以指数方式增加,直到时间段结束。

<!-- This example shows how to use the SizeAnimationUsingKeyFrames to animate the
size of an ArcSegment. -->
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >

    <Canvas HorizontalAlignment="Left" Margin="0" >

      <!-- Create an arc on the screen that animates its size when it loads. -->
      <Path Stroke="Black" StrokeThickness="2" >
        <Path.Data>
          <PathGeometry>
            <PathGeometry.Figures>
              <PathFigureCollection>
                <PathFigure StartPoint="100,200">
                  <PathFigure.Segments>
                    <PathSegmentCollection>
                      <ArcSegment x:Name="myArcSegment" Size="90,80" 
                      SweepDirection="Clockwise"  Point="500,200" />
                    </PathSegmentCollection>
                  </PathFigure.Segments>
                </PathFigure>
              </PathFigureCollection>
            </PathGeometry.Figures>
          </PathGeometry>
        </Path.Data>
        <Path.Triggers>
          <EventTrigger RoutedEvent="Path.Loaded">
            <BeginStoryboard Name="myBeginStoryBoard">
              <Storyboard>
                
                <!-- Animating the Size property uses 3 KeyFrames. -->
                <SizeAnimationUsingKeyFrames
                Storyboard.TargetName="myArcSegment"
                Storyboard.TargetProperty="Size" >
                  <SizeAnimationUsingKeyFrames.KeyFrames>
                    <!-- Using a LinearSizeKeyFrame, the size of the arc increases
                         gradually over the first half second of the animation. -->
                    <LinearSizeKeyFrame KeyTime="0:0:0.5" Value="120,120" />

                    <!-- Using a DiscreteSizeKeyFrame, the size increases suddenly
                    after the first second of the animation. -->
                    <DiscreteSizeKeyFrame KeyTime="0:0:1" Value="150,150" />

                    <!-- Using a SplineSizeKeyFrame, the Size increases slowly at first 
                         and then speeds up exponentially. This KeyFrame takes 2 seconds. -->
                    <SplineSizeKeyFrame KeySpline="0.6,0.0 0.9,0.00" KeyTime="0:0:3" Value="300,300" />

                  </SizeAnimationUsingKeyFrames.KeyFrames>
                </SizeAnimationUsingKeyFrames>

              </Storyboard>
            </BeginStoryboard>
          </EventTrigger>
        </Path.Triggers>
      </Path>
    </Canvas>

</Page>

有关完整示例,请参阅关键帧动画示例

另请参阅