次の方法で共有


方法 : キー フレームを使用してオブジェクトをアニメーション化する

更新 : 2007 年 11 月

この例では、Page コントロールの Background プロパティであるオブジェクトを、キー フレームを使用してアニメーション化する方法について説明します。

使用例

次の例では、ObjectAnimationUsingKeyFrames クラスを使用して、Page コントロールの Background プロパティの色の変更をアニメーション化します。この例のアニメーションは、別の背景ブラシに定期的に変化します。このアニメーションは、DiscreteObjectKeyFrame クラスを使用して 3 つの異なるキー フレームを作成します。次の方法でキー フレームを使用します。

  1. 最初の 1 秒間の終わりに、LinearGradientBrush クラスのインスタンスをアニメーション化します。このセクションでは、線形グラデーションを背景色に適用して、その色を黄色からオレンジ、赤へと変化させます。

  2. 2 秒目の終わりに、RadialGradientBrush クラスのインスタンスをアニメーション化します。このセクションでは、放射状グラデーションを背景色に適用して、その色を白から青、黒へと変化させます。

  3. 3 秒目の終わりに、DrawingBrush クラスのインスタンスをアニメーション化します。このセクションでは、背景に市松模様を適用します。

  4. アニメーションが再度始まり、無制限に繰り返します。

ms752099.alert_note(ja-jp,VS.90).gifメモ :

DiscreteObjectKeyFrame は、ObjectAnimationUsingKeyFrames クラスで使用できるキー フレームの唯一の型です。DiscreteObjectKeyFrame のようなキー フレームは、値の突然の変化を作成するので、この例での色の変化は突然起こります。

<Page 
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" >
    <Page.Triggers>
      <EventTrigger RoutedEvent="Page.Loaded">
        <BeginStoryboard>
          <Storyboard>

            <!-- ObjectAnimationUsingKeyFrames is used to animate properties that take
                 an object as a value. This animation lasts for 4 seconds using 3 KeyFrames which
                 swap different brush objects at regular intervals, making the background of the Page
                 change. -->
            <ObjectAnimationUsingKeyFrames
              Storyboard.TargetProperty="Background"
              Duration="0:0:4" RepeatBehavior="Forever">
            <ObjectAnimationUsingKeyFrames.KeyFrames>

              <!-- Note: Only discrete interpolation (DiscreteObjectKeyFrame) is available for 
              use with ObjectAnimationUsingKeyFrames which merely swaps objects according to
              a specified timeline. Other types of interpolation are too problematic to apply
              to objects.  -->

              <!-- Using a DiscreteObjectKeyFrame, the Page Background suddenly changes 
                   to a LinearGradientBrush after the first second of the animation. -->
              <DiscreteObjectKeyFrame KeyTime="0:0:1">
                <DiscreteObjectKeyFrame.Value>
                  <LinearGradientBrush>
                    <LinearGradientBrush.GradientStops>
                      <GradientStop Color="Yellow" Offset="0.0" />
                      <GradientStop Color="Orange" Offset="0.5" />
                      <GradientStop Color="Red" Offset="1.0" />
                    </LinearGradientBrush.GradientStops>
                  </LinearGradientBrush>
                </DiscreteObjectKeyFrame.Value>
              </DiscreteObjectKeyFrame>

              <!-- Using a DiscreteObjectKeyFrame, the Page Background suddenly changes 
                   to a RadialGradientBrush after the second second of the animation. -->
              <DiscreteObjectKeyFrame KeyTime="0:0:2">
                <DiscreteObjectKeyFrame.Value>
                  <RadialGradientBrush GradientOrigin="0.75,0.25">
                    <RadialGradientBrush.GradientStops>
                      <GradientStop Color="White" Offset="0.0" />
                      <GradientStop Color="MediumBlue" Offset="0.5" />
                      <GradientStop Color="Black" Offset="1.0" />
                    </RadialGradientBrush.GradientStops>
                  </RadialGradientBrush>
                </DiscreteObjectKeyFrame.Value>
              </DiscreteObjectKeyFrame>

              <!-- Using a DiscreteObjectKeyFrame, the Page Background suddenly 
                   changes to a DrawingBrush (creates a checkerboard pattern) after the 
                   third second of the animation. -->
              <DiscreteObjectKeyFrame KeyTime="0:0:3">
                <DiscreteObjectKeyFrame.Value>
                  <DrawingBrush Viewport="0,0,0.25,0.25" TileMode="Tile">
                    <DrawingBrush.Drawing>
                      <DrawingGroup>
                        <DrawingGroup.Children>
                          <GeometryDrawing Brush="White">
                            <GeometryDrawing.Geometry>
                              <RectangleGeometry Rect="0,0,1,1" />
                            </GeometryDrawing.Geometry>
                          </GeometryDrawing>
                          <GeometryDrawing Brush="Black"
                           Geometry="M 0,0 L0,0.5 0.5,0.5 0.5,1 1,1 1,0.5 0.5,0.5 0.5,0" />
                        </DrawingGroup.Children>
                      </DrawingGroup>
                    </DrawingBrush.Drawing>
                  </DrawingBrush>
                </DiscreteObjectKeyFrame.Value>
              </DiscreteObjectKeyFrame>          
            </ObjectAnimationUsingKeyFrames.KeyFrames>
          </ObjectAnimationUsingKeyFrames>        
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Page.Triggers>
</Page>

サンプル全体については、「KeyFrame アニメーションのサンプル」を参照してください。

参照

処理手順

KeyFrame アニメーションのサンプル

概念

キー フレーム アニメーションの概要

参照

ObjectAnimationUsingKeyFrames

Background

Page

DiscreteObjectKeyFrame

LinearGradientBrush

RadialGradientBrush

DrawingBrush

その他の技術情報

キー フレーム アニメーションに関する「方法」トピック