路径动画概述

本主题介绍路径动画,路径动画使您能够使用几何路径来生成输出值。 要沿复杂的路径移动和旋转对象,路径动画十分有用。

系统必备组件

若要了解本主题,您应当熟悉 WPF 动画功能。 有关动画功能的简介,请参见动画概述

由于您使用 PathGeometry 对象来定义路径动画,因此还应当熟悉 PathGeometry 和不同类型的 PathSegment 对象。 有关更多信息,请参见 Geometry 概述

什么是路径动画?

路径动画是一种 AnimationTimeline,它使用 PathGeometry 作为其输入。 您将定义几何路径并使用它来设置路径动画的 PathGeometry 属性,而不是(像在 From/To/By 动画中那样)设置 From、To 或 By 属性或者(像为关键帧动画使用一样)使用关键帧。 在路径动画运行时,它将从路径中读取 x、y 和角度信息,并使用该信息来生成其输出。

要沿复杂的路径使对象产生动画效果,路径动画非常有用。 沿路径移动对象的一种方法是使用 MatrixTransformMatrixAnimationUsingPath 沿复杂路径变换对象。 下面的示例演示了此技术,方法是使用 MatrixAnimationUsingPath 对象对 MatrixTransformMatrix 属性进行动画处理。 MatrixTransform 将应用于按钮,并使按钮沿曲线路径移动。 由于 DoesRotateWithTangent 属性设置为 true,因此矩形将沿路径的切线旋转。

<Page 
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
  xmlns:PresentationOptions="https://schemas.microsoft.com/winfx/2006/xaml/presentation/options" 
  xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="PresentationOptions" Margin="20">
  <Canvas Width="400" Height="400">

    <!-- The Button that is animated across the screen by animating
         the MatrixTransform applied to the button. -->
    <Button MinWidth="100" Content="A Button">
      <Button.RenderTransform>
        <MatrixTransform x:Name="ButtonMatrixTransform">
          <MatrixTransform.Matrix >
            <Matrix />
          </MatrixTransform.Matrix>
        </MatrixTransform>
      </Button.RenderTransform>
      <Button.Triggers>
        <EventTrigger RoutedEvent="Button.Loaded">
          <BeginStoryboard>
            <Storyboard>
              <MatrixAnimationUsingPath
              Storyboard.TargetName="ButtonMatrixTransform"
              Storyboard.TargetProperty="Matrix"
              DoesRotateWithTangent="True"
              Duration="0:0:5" 
              RepeatBehavior="Forever" >
                <MatrixAnimationUsingPath.PathGeometry>
                  <PathGeometry 
                    Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100" 
                    PresentationOptions:Freeze="True" />
                </MatrixAnimationUsingPath.PathGeometry>
              </MatrixAnimationUsingPath>
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Button.Triggers>
    </Button>
  </Canvas>
</Page>

Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Navigation
Imports System.Windows.Shapes


Namespace SDKSample

    ''' <summary>
    ''' Shows how to animate an object along
    ''' a geometric path.
    ''' </summary>
    Public Class MatrixAnimationUsingPathDoesRotateWithTangentExample
        Inherits Page

        Public Sub New()
            Me.Margin = New Thickness(20)

            ' Create a NameScope for the page so that
            ' we can use Storyboards.
            NameScope.SetNameScope(Me, New NameScope())

            ' Create a button.
            Dim aButton As New Button()
            aButton.MinWidth = 100
            aButton.Content = "A Button"

            ' Create a MatrixTransform. This transform
            ' will be used to move the button.
            Dim buttonMatrixTransform As New MatrixTransform()
            aButton.RenderTransform = buttonMatrixTransform

            ' Register the transform's name with the page
            ' so that it can be targeted by a Storyboard.
            Me.RegisterName("ButtonMatrixTransform", buttonMatrixTransform)

            ' Create a Canvas to contain the button
            ' and add it to the page.
            ' Although this example uses a Canvas,
            ' any type of panel will work.
            Dim mainPanel As New Canvas()
            mainPanel.Width = 400
            mainPanel.Height = 400
            mainPanel.Children.Add(aButton)
            Me.Content = mainPanel

            ' Create the animation path.
            Dim animationPath As New PathGeometry()
            Dim pFigure As New PathFigure()
            pFigure.StartPoint = New Point(10, 100)
            Dim pBezierSegment As New PolyBezierSegment()
            pBezierSegment.Points.Add(New Point(35, 0))
            pBezierSegment.Points.Add(New Point(135, 0))
            pBezierSegment.Points.Add(New Point(160, 100))
            pBezierSegment.Points.Add(New Point(180, 190))
            pBezierSegment.Points.Add(New Point(285, 200))
            pBezierSegment.Points.Add(New Point(310, 100))
            pFigure.Segments.Add(pBezierSegment)
            animationPath.Figures.Add(pFigure)

            ' Freeze the PathGeometry for performance benefits.
            animationPath.Freeze()

            ' Create a MatrixAnimationUsingPath to move the
            ' button along the path by animating
            ' its MatrixTransform.
            Dim matrixAnimation As New MatrixAnimationUsingPath()
            matrixAnimation.PathGeometry = animationPath
            matrixAnimation.Duration = TimeSpan.FromSeconds(5)
            matrixAnimation.RepeatBehavior = RepeatBehavior.Forever

            ' Set the animation's DoesRotateWithTangent property
            ' to true so that rotates the rectangle in addition
            ' to moving it.
            matrixAnimation.DoesRotateWithTangent = True

            ' Set the animation to target the Matrix property
            ' of the MatrixTransform named "ButtonMatrixTransform".
            Storyboard.SetTargetName(matrixAnimation, "ButtonMatrixTransform")
            Storyboard.SetTargetProperty(matrixAnimation, New PropertyPath(MatrixTransform.MatrixProperty))

            ' Create a Storyboard to contain and apply the animation.
            Dim pathAnimationStoryboard As New Storyboard()
            pathAnimationStoryboard.Children.Add(matrixAnimation)

            ' Start the storyboard when the button is loaded.
            AddHandler aButton.Loaded, Sub(sender As Object, e As RoutedEventArgs) pathAnimationStoryboard.Begin(Me)



        End Sub
    End Class
End Namespace
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace SDKSample
{

    /// <summary>
    /// Shows how to animate an object along
    /// a geometric path.
    /// </summary>
    public class MatrixAnimationUsingPathDoesRotateWithTangentExample : Page
    {

        public MatrixAnimationUsingPathDoesRotateWithTangentExample()
        {
            this.Margin = new Thickness(20);

            // Create a NameScope for the page so that
            // we can use Storyboards.
            NameScope.SetNameScope(this, new NameScope());

            // Create a button.
            Button aButton = new Button();
            aButton.MinWidth = 100;
            aButton.Content = "A Button";

            // Create a MatrixTransform. This transform
            // will be used to move the button.
            MatrixTransform buttonMatrixTransform = new MatrixTransform();
            aButton.RenderTransform = buttonMatrixTransform;

            // Register the transform's name with the page
            // so that it can be targeted by a Storyboard.
            this.RegisterName("ButtonMatrixTransform", buttonMatrixTransform);

            // Create a Canvas to contain the button
            // and add it to the page.
            // Although this example uses a Canvas,
            // any type of panel will work.
            Canvas mainPanel = new Canvas();
            mainPanel.Width = 400;
            mainPanel.Height = 400;
            mainPanel.Children.Add(aButton);
            this.Content = mainPanel;

            // Create the animation path.
            PathGeometry animationPath = new PathGeometry();
            PathFigure pFigure = new PathFigure();
            pFigure.StartPoint = new Point(10, 100);
            PolyBezierSegment pBezierSegment = new PolyBezierSegment();
            pBezierSegment.Points.Add(new Point(35, 0));
            pBezierSegment.Points.Add(new Point(135, 0));
            pBezierSegment.Points.Add(new Point(160, 100));
            pBezierSegment.Points.Add(new Point(180, 190));
            pBezierSegment.Points.Add(new Point(285, 200));
            pBezierSegment.Points.Add(new Point(310, 100));
            pFigure.Segments.Add(pBezierSegment);
            animationPath.Figures.Add(pFigure);

            // Freeze the PathGeometry for performance benefits.
            animationPath.Freeze();

            // Create a MatrixAnimationUsingPath to move the
            // button along the path by animating
            // its MatrixTransform.
            MatrixAnimationUsingPath matrixAnimation =
                new MatrixAnimationUsingPath();
            matrixAnimation.PathGeometry = animationPath;
            matrixAnimation.Duration = TimeSpan.FromSeconds(5);
            matrixAnimation.RepeatBehavior = RepeatBehavior.Forever;

            // Set the animation's DoesRotateWithTangent property
            // to true so that rotates the rectangle in addition
            // to moving it.
            matrixAnimation.DoesRotateWithTangent = true;

            // Set the animation to target the Matrix property
            // of the MatrixTransform named "ButtonMatrixTransform".
            Storyboard.SetTargetName(matrixAnimation, "ButtonMatrixTransform");
            Storyboard.SetTargetProperty(matrixAnimation, 
                new PropertyPath(MatrixTransform.MatrixProperty));

            // Create a Storyboard to contain and apply the animation.
            Storyboard pathAnimationStoryboard = new Storyboard();
            pathAnimationStoryboard.Children.Add(matrixAnimation);

            // Start the storyboard when the button is loaded.
            aButton.Loaded += delegate(object sender, RoutedEventArgs e)
            {
                // Start the storyboard.
                pathAnimationStoryboard.Begin(this);
            };



        }  
    }
}

有关在 XAML 示例中使用的路径语法的更多信息,请参见路径标记语法概述。有关完整示例,请参见Path Animation Sample(路径动画示例)。

您可以通过在 XAML 和代码中使用 Storyboard,或者通过在代码中使用 BeginAnimation 方法将路径动画应用于属性。 您还可以使用路径动画创建 AnimationClock,并将其应用于一个和多个属性。 有关应用动画的不同方法的更多信息,请参见属性动画技术概述

路径动画类型

由于动画生成属性值,因此对于不同的属性类型,会有不同的动画类型。 若要对采用 Double 的属性(比如TranslateTransformX属性)进行动画处理,请使用生成 Double 值的动画。 若要对采用 Point 的属性进行动画处理,请使用生成 Point 值的动画,依此类推。

路径动画类属于 System.Windows.Media.Animation 命名空间,并使用下列命名约定:

*<类型>*AnimationUsingPath

其中 <类型> 是该类进行动画处理的值的类型。

WPF 提供下列路径动画类。

属性类型

对应的路径动画类

示例

Double

DoubleAnimationUsingPath

如何:沿着路径针对对象进行动画处理(双重动画)

Matrix

MatrixAnimationUsingPath

如何:沿着路径针对对象进行动画处理(矩阵动画)

Point

PointAnimationUsingPath

如何:沿着路径针对对象进行动画处理(点动画)

MatrixAnimationUsingPath 依据其 PathGeometry 生成 Matrix。 在与 MatrixTransform 一起使用时,MatrixAnimationUsingPath 可以沿路径移动对象。 如果将 MatrixAnimationUsingPathDoesRotateWithTangent 属性设置为 true,它还可以沿路径的曲线旋转对象。

PointAnimationUsingPath 依据其 PathGeometry 的 x 和 y 坐标生成 Point 值。 通过使用 PointAnimationUsingPath 对采用 Point 值的属性进行动画处理,您可以沿路径移动对象。 PointAnimationUsingPath 无法旋转对象。

DoubleAnimationUsingPath 依据其 PathGeometry 生成 Double。 通过设置 Source 属性,您可以指定 DoubleAnimationUsingPath 是否使用路径的 x 坐标、y 坐标或角度作为其输出。 您可以使用 DoubleAnimationUsingPath 来旋转对象或者沿 x 轴或 y 轴移动对象。

路径动画输入

每个路径动画类都提供了一个用于指定其输入的 PathGeometry 属性。 路径动画使用 PathGeometry 来生成其输出值。 利用 PathGeometry 类,您可以描述由弧线、曲线和直线组成的多个复杂图形。

PathGeometry 的核心是 PathFigure 对象的集合;这些对象之所以采用这种命名方式,原因是每个图形都描述 PathGeometry 中的一个离散形状。 每个 PathFigure 都由一个或多个 PathSegment 对象组成,其中每个对象都描述图形的一条线段。

线段有多种类型。

线段类型

说明

ArcSegment

在两个点之间创建一条椭圆弧线。

BezierSegment

在两个点之间创建一条三次方贝塞尔曲线。

LineSegment

在两个点之间创建一条直线。

PolyBezierSegment

创建一系列三次方贝塞尔曲线。

PolyLineSegment

创建一系列直线。

PolyQuadraticBezierSegment

创建一系列二次贝塞尔曲线。

QuadraticBezierSegment

创建一条二次贝塞尔曲线。

PathFigure 中的线段将合并为一个几何形状,该形状使用一条线段的终点作为下一条线段的起点。 PathFigureStartPoint 属性指定绘制第一条线段的起始点。 后面的每条线段都以上一条线段的终点作为起点。 例如,通过将 StartPoint 属性设置为 10,50 并创建 Point 属性设置为 10,150 的 LineSegment,可定义一条从 10,50 到 10,150 的竖线。

有关 PathGeometry 对象的更多信息,请参见Geometry 概述

在 XAML 中,您还可以使用特殊的缩写语法来设置 PathGeometryFigures 属性。 有关更多信息,请参见路径标记语法概述。

有关 XAML 示例中使用的路径语法的更多信息,请参见路径标记语法概述。

请参见

概念

路径标记语法

动画概述

属性动画技术概述

其他资源

Path Animation Sample

路径动画帮助主题