如何:使用关键帧对边框的粗细进行动画处理

此示例演示如何对 BorderBorderThickness 属性进行动画处理。

示例

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

  1. 在最初的半秒内,将使用 LinearThicknessKeyFrame 类的实例逐渐增加边框的粗细。 此示例使用 LinearThicknessKeyFrame 在值之间创建平滑的线性递增。

  2. 在接下来的半秒结束时,将使用 DiscreteThicknessKeyFrame 类的实例突然增加边框的粗细。 离散关键帧(如从 DiscreteThicknessKeyFrame 派生的那些关键帧)将在值之间创建突然跳转,使动画出现跳帧效果。

  3. 在最后两秒内,将使用 SplineThicknessKeyFrame 类的实例减小边框的粗细。 样条关键帧与从 SplineThicknessKeyFrame 派生的关键帧一样,根据 KeySpline 属性的值在各个值之间创建可变过渡。 在此关键帧中,动画一开始较慢,然后以指数级加速,直至时间段结束。

<!-- This example shows how to use the ThicknessAnimationUsingKeyFrames to create
an animation on the BorderThickness property of a Border. -->
<Page  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="Microsoft.Samples.KeyFrameExamples.ThicknessAnimationUsingKeyFramesExample"
  Name="myRootElement"
  WindowTitle="ThicknessAnimationUsingKeyFrames Example">

  <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
    <Border Background="#99FFFFFF" BorderBrush="#CCCCFF" BorderThickness="1"
    Margin="0,60,0,20" Padding="20"  >
      <Border.Triggers>
        <EventTrigger RoutedEvent="Border.Loaded">
          <BeginStoryboard>
            <Storyboard>

              <!-- Animating the BorderThickness property uses 3 KeyFrames. -->
              <ThicknessAnimationUsingKeyFrames
                Storyboard.TargetProperty="BorderThickness"
                Duration="0:0:5" FillBehavior="HoldEnd" RepeatBehavior="Forever">
                <ThicknessAnimationUsingKeyFrames.KeyFrames>

                  <!-- Using a LinearThicknessKeyFrame, thickness increases gradually
                  over the first half second of the animation. -->
                  <LinearThicknessKeyFrame KeyTime="0:0:0.5">
                    <LinearThicknessKeyFrame.Value>
                      <Thickness Left="8" Right="8" Top="6" Bottom="6" />
                    </LinearThicknessKeyFrame.Value>
                  </LinearThicknessKeyFrame>

                  <!-- Using a DiscreteThicknessKeyFrame, thickness increases suddenly
                  after the first second of the animation. -->
                  <DiscreteThicknessKeyFrame KeyTime="0:0:1">
                    <DiscreteThicknessKeyFrame.Value>
                      <Thickness Left="28" Right="28" Top="24" Bottom="24" />
                    </DiscreteThicknessKeyFrame.Value>
                  </DiscreteThicknessKeyFrame>

                  <!-- Using a SplineThicknessKeyFrame, thickness decreases slowly at first
                  and then suddenly contracts. This KeyFrame takes 2 seconds. -->
                  <SplineThicknessKeyFrame KeySpline="0.6,0.0 0.9,0.00" KeyTime="0:0:3">
                    <SplineThicknessKeyFrame.Value>
                      <Thickness Left="1" Right="1" Top="1" Bottom="8" />
                    </SplineThicknessKeyFrame.Value>
                  </SplineThicknessKeyFrame>

                </ThicknessAnimationUsingKeyFrames.KeyFrames>
              </ThicknessAnimationUsingKeyFrames>
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Border.Triggers>

      <TextBlock>
        This example shows how to use the ThicknessAnimationUsingKeyFrames to create
        an animation on the BorderThickness property of a Border. 
      </TextBlock>
    </Border>

  </StackPanel>
</Page>

有关完整示例,请参见 KeyFrame Animation Sample(KeyFrame 动画示例)。

请参见

任务

如何:对 BorderThickness 值进行动画处理

参考

LinearThicknessKeyFrame

DiscreteThicknessKeyFrame

SplineThicknessKeyFrame

概念

关键帧动画概述

其他资源

关键帧动画帮助主题