次の方法で共有


From/To/By アニメーションの概要

このトピックでは、From/To/By アニメーションを使用して依存関係プロパティをアニメーション化する方法について説明します。 From/To/By アニメーションでは、2 つの値の間に遷移が作成されます。

[前提条件]

このトピックを理解するには、WPF アニメーションの機能について理解している必要があります。 アニメーション機能の概要については、「 アニメーションの概要」を参照してください。

From/To/By アニメーションとは

From/To/By アニメーションは、開始値と終了値の間の遷移を作成する AnimationTimeline の一種です。 切り替えが完了するまでの時間は、そのアニメーションの Duration によって決まります。

From/To/By アニメーションをプロパティに適用するには、マークアップとコードの Storyboard を使用するか、コードで BeginAnimation メソッドを使用します。 From/To/By アニメーションを使用して AnimationClock を作成し、1 つ以上のプロパティに適用することもできます。 アニメーションを適用するためのさまざまな方法の詳細については、「 プロパティ アニメーション手法の概要」を参照してください。

From/To/By アニメーションには、2 つ以下のターゲット値を指定できます。 2 つ以上のターゲット値を持つアニメーションが必要な場合は、キー フレーム アニメーションを使用します。 キー フレーム アニメーションについては、「 Key-Frame アニメーションの概要」を参照してください。

「開始」、「終了」、「設定」アニメーションタイプ

アニメーションはプロパティ値を生成するため、プロパティの種類ごとに異なるアニメーションの種類があります。 要素のDouble プロパティなど、Widthを受け取るプロパティをアニメーション化するには、Double値を生成するアニメーションを使用します。 Pointを受け取るプロパティをアニメーション化するには、Point値を生成するアニメーションなどを使用します。

From/To/By アニメーション クラスは、 System.Windows.Media.Animation 名前空間に属し、次の名前付け規則を使用します。

<種類>Animation

ここで、<Type> クラスがアニメーション化する値の型です。

WPF には、次の From/To/By アニメーション クラスが用意されています。

プロパティの種類 対応する From/To/By アニメーション クラス
Byte ByteAnimation
Color ColorAnimation
Decimal DecimalAnimation
Double DoubleAnimation
Int16 Int16Animation
Int32 Int32Animation
Int64 Int64Animation
Point PointAnimation
Quaternion QuaternionAnimation
Rect RectAnimation
Rotation3D Rotation3DAnimation
Single SingleAnimation
Size SizeAnimation
Thickness ThicknessAnimation
Vector3D Vector3DAnimation
Vector VectorAnimation

ターゲット値

From/To/By アニメーションでは、2 つのターゲット値の間に遷移が作成されます。 開始値 ( From プロパティを使用して設定) と終了値 ( To プロパティを使用して設定) を指定するのが一般的です。 ただし、開始値、宛先値、またはオフセット値のみを指定することもできます。 このような場合、アニメーションは、アニメーション化されているプロパティから不足しているターゲット値を取得します。 次の一覧では、アニメーションのターゲット値を指定するさまざまな方法について説明します。

  • 開始値

    アニメーションの開始値を明示的に指定する場合は、 From プロパティを使用します。 From プロパティは単独で使用することも、Toプロパティまたは By プロパティと共に使用することもできます。 Fromプロパティのみを指定した場合、アニメーションはその値からアニメーション化されたプロパティの基本値に遷移します。

  • 終了値

    アニメーションの終了値を指定するには、その To プロパティを使用します。 To プロパティを単独で使用する場合、アニメーションは、アニメーション化されているプロパティから、または同じプロパティに適用されている別のアニメーションの出力から開始値を取得します。 To プロパティを From プロパティと共に使用して、アニメーションの開始値と終了値を明示的に指定できます。

  • オフセット値

    By プロパティを使用すると、アニメーションの明示的な開始値または終了値の代わりにオフセットを指定できます。 アニメーションの By プロパティは、アニメーションがその期間にわたって値を変更する量によって指定されます。 By プロパティは、単独で使用することも、From プロパティと共に使用することもできます。 By プロパティのみを指定した場合、アニメーションはプロパティの基本値または別のアニメーションの出力にオフセット値を追加します。

From/To/By 値の使用

以降のセクションでは、 FromTo、および By プロパティを一緒に、または個別に使用する方法について説明します。

このセクションの例では、それぞれ、DoubleAnimation (From/To/By アニメーションの一種) を使用して、デバイスに依存しない 10 ピクセルの高さと 100 デバイスに依存しないピクセル幅のWidthRectangle プロパティをアニメーション化します。

各例では DoubleAnimationを使用していますが、すべての From/To/By アニメーションの From、To、By の各プロパティは同じように動作します。 これらの各例では Storyboardを使用していますが、From/To/By アニメーションは他の方法で使用できます。 詳細については、「 プロパティ アニメーション手法の概要」を参照してください。

発信 / 受信

FromToの値を一緒に設定すると、From プロパティで指定された値から、To プロパティで指定された値までアニメーションが進行します。

次の使用例は、FromDoubleAnimation プロパティを 50 に設定し、そのTo プロパティを 300 に設定します。 その結果、WidthRectangleは 50 から 300 にアニメーション化されます。

// Demonstrates the From and To properties used together.

// Create a NameScope for this page so that
// Storyboards can be used.
NameScope.SetNameScope(this, new NameScope());

Rectangle myRectangle = new Rectangle();

// Assign the Rectangle a name so that
// it can be targeted by a Storyboard.
this.RegisterName(
    "fromToAnimatedRectangle", myRectangle);
myRectangle.Height = 10;
myRectangle.Width = 100;
myRectangle.HorizontalAlignment = HorizontalAlignment.Left;
myRectangle.Fill = Brushes.Black;

// Demonstrates the From and To properties used together.
// Animates the rectangle's Width property from 50 to 300 over 10 seconds.
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 50;
myDoubleAnimation.To = 300;
myDoubleAnimation.Duration =
    new Duration(TimeSpan.FromSeconds(10));

Storyboard.SetTargetName(myDoubleAnimation, "fromToAnimatedRectangle");
Storyboard.SetTargetProperty(myDoubleAnimation,
    new PropertyPath(Rectangle.WidthProperty));
Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);

// Use an anonymous event handler to begin the animation
// when the rectangle is clicked.
myRectangle.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs args)
    {
        myStoryboard.Begin(myRectangle);
    };
' Demonstrates the From and To properties used together.

' Create a NameScope for this page so that
' Storyboards can be used.
NameScope.SetNameScope(Me, New NameScope())

Dim myRectangle As New Rectangle()

' Assign the Rectangle a name so that
' it can be targeted by a Storyboard.
Me.RegisterName("fromToAnimatedRectangle", myRectangle)
myRectangle.Height = 10
myRectangle.Width = 100
myRectangle.HorizontalAlignment = HorizontalAlignment.Left
myRectangle.Fill = Brushes.Black

' Demonstrates the From and To properties used together.
' Animates the rectangle's Width property from 50 to 300 over 10 seconds.
Dim myDoubleAnimation As New DoubleAnimation()
myDoubleAnimation.From = 50
myDoubleAnimation.To = 300
myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(10))

Storyboard.SetTargetName(myDoubleAnimation, "fromToAnimatedRectangle")
Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Rectangle.WidthProperty))
Dim myStoryboard As New Storyboard()
myStoryboard.Children.Add(myDoubleAnimation)

' Use an anonymous event handler to begin the animation
' when the rectangle is clicked.
AddHandler myRectangle.MouseLeftButtonDown, Sub(sender As Object, args As MouseButtonEventArgs) myStoryboard.Begin(myRectangle)

移行先

To プロパティのみを設定すると、アニメーションは、アニメーション化されたプロパティの基本値から、または同じプロパティに以前に適用された作成アニメーションの出力から、To プロパティで指定された値まで進行します。

("アニメーションの作成" は、Activeハンドオフ動作を使用して現在のアニメーションが適用されたときに有効なのと同じプロパティに以前に適用されたFillingまたはComposeアニメーションを指します。

次の例では、ToDoubleAnimation プロパティのみを 300 に設定します。 開始値が指定されていないため、 DoubleAnimation は、 Width プロパティの基本値 (100) を開始値として使用します。 WidthRectangleは、100 からアニメーションのターゲット値 300 までアニメーション化されます。

// Demonstrates the use of the To property.

// Create a NameScope for this page so that
// Storyboards can be used.
NameScope.SetNameScope(this, new NameScope());

Rectangle myRectangle = new Rectangle();

// Assign the Rectangle a name so that
// it can be targeted by a Storyboard.
this.RegisterName(
    "toAnimatedRectangle", myRectangle);
myRectangle.Height = 10;
myRectangle.Width = 100;
myRectangle.HorizontalAlignment = HorizontalAlignment.Left;
myRectangle.Fill = Brushes.Gray;

// Demonstrates the To property used by itself. Animates
// the Rectangle's Width property from its base value
// (100) to 300 over 10 seconds.
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.To = 300;
myDoubleAnimation.Duration =
    new Duration(TimeSpan.FromSeconds(10));

Storyboard.SetTargetName(myDoubleAnimation, "toAnimatedRectangle");
Storyboard.SetTargetProperty(myDoubleAnimation,
    new PropertyPath(Rectangle.WidthProperty));
Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);

// Use an anonymous event handler to begin the animation
// when the rectangle is clicked.
myRectangle.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs args)
    {
        myStoryboard.Begin(myRectangle);
    };
' Demonstrates the use of the To property.

' Create a NameScope for this page so that
' Storyboards can be used.
NameScope.SetNameScope(Me, New NameScope())

Dim myRectangle As New Rectangle()

' Assign the Rectangle a name so that
' it can be targeted by a Storyboard.
Me.RegisterName("toAnimatedRectangle", myRectangle)
myRectangle.Height = 10
myRectangle.Width = 100
myRectangle.HorizontalAlignment = HorizontalAlignment.Left
myRectangle.Fill = Brushes.Gray

' Demonstrates the To property used by itself. Animates
' the Rectangle's Width property from its base value
' (100) to 300 over 10 seconds.
Dim myDoubleAnimation As New DoubleAnimation()
myDoubleAnimation.To = 300
myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(10))

Storyboard.SetTargetName(myDoubleAnimation, "toAnimatedRectangle")
Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Rectangle.WidthProperty))
Dim myStoryboard As New Storyboard()
myStoryboard.Children.Add(myDoubleAnimation)

' Use an anonymous event handler to begin the animation
' when the rectangle is clicked.
AddHandler myRectangle.MouseLeftButtonDown, Sub(sender As Object, args As MouseButtonEventArgs) myStoryboard.Begin(myRectangle)

によって

アニメーションの By プロパティのみを設定すると、アニメーションは、アニメーション化されているプロパティの基本値から、作成中のアニメーションの出力から、その値と By プロパティで指定された値の合計まで進行します。

次の例では、ByDoubleAnimation プロパティのみを 300 に設定します。 この例では開始値が指定されていないため、 DoubleAnimation では、 Width プロパティ 100 の基本値が開始値として使用されます。 終了値は、開始値の100にアニメーションのBy値である300を加えることによって決定されます。結果は400になります。 その結果、WidthRectangleは 100 から 400 にアニメーション化されます。

// Demonstrates the use of the By property.

// Create a NameScope for this page so that
// Storyboards can be used.
NameScope.SetNameScope(this, new NameScope());

Rectangle myRectangle = new Rectangle();

// Assign the Rectangle a name so that
// it can be targeted by a Storyboard.
this.RegisterName(
    "byAnimatedRectangle", myRectangle);
myRectangle.Height = 10;
myRectangle.Width = 100;
myRectangle.HorizontalAlignment = HorizontalAlignment.Left;
myRectangle.Fill = Brushes.RoyalBlue;

// Demonstrates the By property used by itself.
// Increments the Rectangle's Width property by 300 over 10 seconds.
// As a result, the Width property is animated from its base value
// (100) to 400 (100 + 300) over 10 seconds.
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.By = 300;
myDoubleAnimation.Duration =
    new Duration(TimeSpan.FromSeconds(10));

Storyboard.SetTargetName(myDoubleAnimation, "byAnimatedRectangle");
Storyboard.SetTargetProperty(myDoubleAnimation,
    new PropertyPath(Rectangle.WidthProperty));
Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);

// Use an anonymous event handler to begin the animation
// when the rectangle is clicked.
myRectangle.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs args)
    {
        myStoryboard.Begin(myRectangle);
    };
' Demonstrates the use of the By property.

' Create a NameScope for this page so that
' Storyboards can be used.
NameScope.SetNameScope(Me, New NameScope())

Dim myRectangle As New Rectangle()

' Assign the Rectangle a name so that
' it can be targeted by a Storyboard.
Me.RegisterName("byAnimatedRectangle", myRectangle)
myRectangle.Height = 10
myRectangle.Width = 100
myRectangle.HorizontalAlignment = HorizontalAlignment.Left
myRectangle.Fill = Brushes.RoyalBlue

' Demonstrates the By property used by itself.
' Increments the Rectangle's Width property by 300 over 10 seconds.
' As a result, the Width property is animated from its base value
' (100) to 400 (100 + 300) over 10 seconds.
Dim myDoubleAnimation As New DoubleAnimation()
myDoubleAnimation.By = 300
myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(10))

Storyboard.SetTargetName(myDoubleAnimation, "byAnimatedRectangle")
Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Rectangle.WidthProperty))
Dim myStoryboard As New Storyboard()
myStoryboard.Children.Add(myDoubleAnimation)

' Use an anonymous event handler to begin the animation
' when the rectangle is clicked.
AddHandler myRectangle.MouseLeftButtonDown, Sub(sender As Object, args As MouseButtonEventArgs) myStoryboard.Begin(myRectangle)

から/によって

アニメーションの From プロパティと By プロパティを設定すると、 From プロパティで指定された値から、 From プロパティと By プロパティの合計で指定された値までアニメーションが進行します。

次の使用例は、FromDoubleAnimation プロパティを 50 に設定し、そのBy プロパティを 300 に設定します。 終了値は、開始値である50にアニメーションのBy値300を加えて計算された350です。 その結果、WidthRectangleは 50 から 350 にアニメーション化されます。

// Demonstrates the use of the From and By properties.

// Create a NameScope for this page so that
// Storyboards can be used.
NameScope.SetNameScope(this, new NameScope());

Rectangle myRectangle = new Rectangle();

// Assign the Rectangle a name so that
// it can be targeted by a Storyboard.
this.RegisterName(
    "byAnimatedRectangle", myRectangle);
myRectangle.Height = 10;
myRectangle.Width = 100;
myRectangle.HorizontalAlignment = HorizontalAlignment.Left;
myRectangle.Fill = Brushes.BlueViolet;

// Demonstrates the From and By properties used together.
// Increments the Rectangle's Width property by 300 over 10 seconds.
// As a result, the Width property is animated from 50
// to 350 (50 + 300) over 10 seconds.
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 50;
myDoubleAnimation.By = 300;
myDoubleAnimation.Duration =
    new Duration(TimeSpan.FromSeconds(10));

Storyboard.SetTargetName(myDoubleAnimation, "byAnimatedRectangle");
Storyboard.SetTargetProperty(myDoubleAnimation,
    new PropertyPath(Rectangle.WidthProperty));
Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);

// Use an anonymous event handler to begin the animation
// when the rectangle is clicked.
myRectangle.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs args)
    {
        myStoryboard.Begin(myRectangle);
    };
' Demonstrates the use of the From and By properties.

' Create a NameScope for this page so that
' Storyboards can be used.
NameScope.SetNameScope(Me, New NameScope())

Dim myRectangle As New Rectangle()

' Assign the Rectangle a name so that
' it can be targeted by a Storyboard.
Me.RegisterName("byAnimatedRectangle", myRectangle)
myRectangle.Height = 10
myRectangle.Width = 100
myRectangle.HorizontalAlignment = HorizontalAlignment.Left
myRectangle.Fill = Brushes.BlueViolet

' Demonstrates the From and By properties used together.
' Increments the Rectangle's Width property by 300 over 10 seconds.
' As a result, the Width property is animated from 50
' to 350 (50 + 300) over 10 seconds.
Dim myDoubleAnimation As New DoubleAnimation()
myDoubleAnimation.From = 50
myDoubleAnimation.By = 300
myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(10))

Storyboard.SetTargetName(myDoubleAnimation, "byAnimatedRectangle")
Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Rectangle.WidthProperty))
Dim myStoryboard As New Storyboard()
myStoryboard.Children.Add(myDoubleAnimation)

' Use an anonymous event handler to begin the animation
' when the rectangle is clicked.
AddHandler myRectangle.MouseLeftButtonDown, Sub(sender As Object, args As MouseButtonEventArgs) myStoryboard.Begin(myRectangle)

より

アニメーションの From 値のみを指定すると、アニメーションは、 From プロパティで指定された値から、アニメーション化されるプロパティの基本値、または作成中のアニメーションの出力まで進行します。

次の例では、FromDoubleAnimation プロパティのみを 50 に設定します。 終了値が指定されていないため、 DoubleAnimation では、 Width プロパティ 100 の基本値が終了値として使用されます。 WidthRectangleは、50 から Width プロパティの基本値 100 までアニメーション化されます。

// Demonstrates the use of the From property.

// Create a NameScope for this page so that
// Storyboards can be used.
NameScope.SetNameScope(this, new NameScope());

Rectangle myRectangle = new Rectangle();

// Assign the Rectangle a name so that
// it can be targeted by a Storyboard.
this.RegisterName(
    "fromAnimatedRectangle", myRectangle);
myRectangle.Height = 10;
myRectangle.Width = 100;
myRectangle.HorizontalAlignment = HorizontalAlignment.Left;
myRectangle.Fill = Brushes.Purple;

// Demonstrates the From property used by itself. Animates the
// rectangle's Width property from 50 to its base value (100)
// over 10 seconds.
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 50;
myDoubleAnimation.Duration =
    new Duration(TimeSpan.FromSeconds(10));

Storyboard.SetTargetName(myDoubleAnimation, "fromAnimatedRectangle");
Storyboard.SetTargetProperty(myDoubleAnimation,
    new PropertyPath(Rectangle.WidthProperty));
Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myDoubleAnimation);

// Use an anonymous event handler to begin the animation
// when the rectangle is clicked.
myRectangle.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs args)
    {
        myStoryboard.Begin(myRectangle);
    };
' Demonstrates the use of the From property.

' Create a NameScope for this page so that
' Storyboards can be used.
NameScope.SetNameScope(Me, New NameScope())

Dim myRectangle As New Rectangle()

' Assign the Rectangle a name so that
' it can be targeted by a Storyboard.
Me.RegisterName("fromAnimatedRectangle", myRectangle)
myRectangle.Height = 10
myRectangle.Width = 100
myRectangle.HorizontalAlignment = HorizontalAlignment.Left
myRectangle.Fill = Brushes.Purple

' Demonstrates the From property used by itself. Animates the
' rectangle's Width property from 50 to its base value (100)
' over 10 seconds.
Dim myDoubleAnimation As New DoubleAnimation()
myDoubleAnimation.From = 50
myDoubleAnimation.Duration = New Duration(TimeSpan.FromSeconds(10))

Storyboard.SetTargetName(myDoubleAnimation, "fromAnimatedRectangle")
Storyboard.SetTargetProperty(myDoubleAnimation, New PropertyPath(Rectangle.WidthProperty))
Dim myStoryboard As New Storyboard()
myStoryboard.Children.Add(myDoubleAnimation)

' Use an anonymous event handler to begin the animation
' when the rectangle is clicked.
AddHandler myRectangle.MouseLeftButtonDown, Sub(sender As Object, args As MouseButtonEventArgs) myStoryboard.Begin(myRectangle)

宛先/差出人

アニメーションの To プロパティと By プロパティの両方を設定した場合、 By プロパティは無視されます。

その他のアニメーションの種類

FROM/To/By アニメーションは、WPF が提供するアニメーションの唯一の種類ではありません。キー フレーム アニメーションとパス アニメーションも提供します。

  • キー フレーム アニメーションは、キー フレームを使用して記述された任意の数のターゲット値に沿ってアニメーション化されます。 詳細については、「 Key-Frame アニメーションの概要」を参照してください。

  • パス アニメーションは、 PathGeometryから出力値を生成します。 詳細については、「 パス アニメーションの概要」を参照してください。

WPF では、独自のカスタム アニメーションの種類を作成することもできます。 詳細については、「 カスタム アニメーションの概要」を参照してください。

こちらも参照ください