PointAnimationUsingPath的介绍

PointAnimationUsingPath 是 WPF 中的一个类,它用于创建一个动画,该动画会沿着指定的路径移动一个点。

关于 PointAnimationUsingPath这些属性比较重要:

属性 类型 说明
PathGeometry PathGeometry 这个属性定义了动画的路径。你可以使用 PathGeometry 类来创建复杂的路径,包括直线、曲线和弧线。
Duration Duration 这个属性定义了动画的持续时间。你可以使用 TimeSpan 类来设置这个属性。
RepeatBehavior RepeatBehavior 这个属性定义了动画的重复行为。例如,你可以设置这个属性为 RepeatBehavior.Forever,这样动画就会无限重复。
AutoReverse Boolean 如果这个属性被设置为 true,那么动画在到达路径的终点后会自动反向播放,回到路径的起点。

PathGeometry的介绍

PathGeometry 是 WPF 中的一个类,它表示一组连接的线和曲线,这些线和曲线可以组成复杂的形状。

关于PathGeometry 等会我们会接触到的属性为Figures

Figures属性介绍如下:

属性名 类型 说明
Figures PathFigureCollection 获取或设置描述路径内容的 PathFigure对象的集合。

PathGeometry 的用法示例

使用PathGeometry绘制一条直线:

PathGeometry pathGeometry = new PathGeometry();

PathFigure figure = new PathFigure();
figure.StartPoint = new Point(0, 50); // 线的起点 LineSegment lineSegment = new LineSegment();
lineSegment.Point = new Point(400, 50); // 线的终点 figure.Segments.Add(lineSegment); pathGeometry.Figures.Add(figure); Path1.Data = pathGeometry;

效果:

使用PathGeometry绘制一个矩形:

 PathGeometry pathGeometry = new PathGeometry();

 PathFigure figure = new PathFigure();
figure.StartPoint = new Point(10, 10); // 矩形的左上角 LineSegment line1 = new LineSegment();
line1.Point = new Point(310, 10); // 矩形的右上角
figure.Segments.Add(line1); LineSegment line2 = new LineSegment();
line2.Point = new Point(310, 160); // 矩形的右下角
figure.Segments.Add(line2); LineSegment line3 = new LineSegment();
line3.Point = new Point(10, 160); // 矩形的左下角
figure.Segments.Add(line3); LineSegment line4 = new LineSegment();
line4.Point = new Point(10, 10); // 矩形的左上角,闭合矩形
figure.Segments.Add(line4); pathGeometry.Figures.Add(figure); Path1.Data = pathGeometry;

效果:

使用PathGeometry绘制一个圆:

 PathFigure figure = new PathFigure();
figure.StartPoint = new Point(150, 50); // 圆的顶点 ArcSegment arc1 = new ArcSegment();
arc1.Point = new Point(150, 150); // 圆的底点
arc1.Size = new Size(50, 50); // 圆的半径
arc1.SweepDirection = SweepDirection.Clockwise;
arc1.IsLargeArc = true; // 表示这是一个大于或等于180度的弧
figure.Segments.Add(arc1); ArcSegment arc2 = new ArcSegment();
arc2.Point = new Point(150, 50); // 圆的顶点,闭合圆
arc2.Size = new Size(50, 50); // 圆的半径
arc2.SweepDirection = SweepDirection.Clockwise;
arc2.IsLargeArc = true; // 表示这是一个大于或等于180度的弧
figure.Segments.Add(arc2); PathGeometry pathGeometry = new PathGeometry();
pathGeometry.Figures.Add(figure); Path1.Data = pathGeometry;

效果:

本示例中PathGeometry的用法

PathGeometry pathGeometry = new PathGeometry();
PathFigureCollection figures = PathFigureCollection.Parse("M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100");
pathGeometry.Figures = figures; Path1.Data = pathGeometry;
PathFigureCollection figures = PathFigureCollection.Parse("M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100");

这行代码使用 PathFigureCollection.Parse 方法解析一个包含路径数据的字符串,并返回一个 PathFigureCollection 对象。这个字符串使用的是 SVG 路径数据格式。在这个例子中,字符串 "M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100" 表示一个开始于 (10,100) 的路径,然后通过三个贝塞尔曲线段(每个曲线段由两个控制点和一个结束点定义)连接到 (160,100)、(310,100) 和 (10,100)。

效果:

Storyboard的介绍

Storyboard 是 WPF 中的一个类,它用于组织和管理一组动画,这些动画可以同时或按照指定的顺序播放。Storyboard 可以用于创建复杂的动画序列,包括并行和顺序动画。

在本示例中,Storyboard 对象被用来存储和控制 PointAnimationUsingPath 对象的播放。你可以调用 Storyboard 的 Begin、Pause、Resume 和 Stop 方法来控制动画的播放。

在本示例中会用到的属性:

属性名 类型 说明
Children TimelineCollection 这是一个动画集合,包含了 Storyboard 中的所有动画。

在本示例中会用到的方法:

方法名 说明
Begin(FrameworkElement, Boolean) 将与此 Storyboard 关联的动画应用到其目标并启动它们。
Pause(FrameworkContentElement) 暂停与此 Storyboard关联的指定 FrameworkContentElement 的Clock。
Resume(FrameworkContentElement) 恢复为此 Storyboard创建的 Clock。
Stop(FrameworkContentElement) 停止为此 Storyboard 创建的 Clock。
Storyboard.SetTargetName(DependencyObject, String) 使指定的 Timeline 面向具有指定名称的依赖属性。
Storyboard.SetTargetProperty(DependencyObject, PropertyPath) 使指定的 Timeline 目标成为指定的依赖属性。

示例

xaml:

<Page x:Class="PointAnimationUsingPathDemo.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PointAnimationUsingPathDemo"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page1"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="StackPanel1">
<Canvas HorizontalAlignment="Center" Width="340" Height="240" x:Name="Canvas1">
<Path VerticalAlignment="Top" Margin="15,15,15,15"
Data="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100"
Stroke="Black" StrokeThickness="2"
Stretch="None" />
<Path Fill="Orange" Margin="15,15,15,15">
<Path.Data>
<!-- Describes an ellipse. -->
<EllipseGeometry x:Name="MyAnimatedEllipseGeometry"
Center="10,100" RadiusX="15" RadiusY="15" />
</Path.Data>
</Path>
</Canvas>
<Button Click="Button_Click_Begin">开始</Button>
<Button Click="Button_Click_Pause">暂停</Button>
<Button Click="Button_Click_Resume">继续</Button>
<Button Click="Button_Click_Stop">停止</Button>
</StackPanel>
</Page>

效果:

cs:

  PathGeometry pathGeometry = new PathGeometry();
PathFigureCollection figures = PathFigureCollection.Parse("M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100");
pathGeometry.Figures = figures;

创建一个PathGeometry对象并设置Figures属性。

// 创建和配置 PointAnimationUsingPath
PointAnimationUsingPath pointAnimation = new PointAnimationUsingPath();
pointAnimation.Duration = TimeSpan.FromSeconds(5);
pointAnimation.RepeatBehavior = RepeatBehavior.Forever;
pointAnimation.AutoReverse = true;
pointAnimation.PathGeometry = pathGeometry;

这段代码创建并配置了一个 PointAnimationUsingPath 对象,该对象表示一个沿路径移动的点动画。这个动画将在5秒内沿着指定的路径播放,播放完毕后将反向播放,然后无限次重复。

Storyboard.SetTargetName(pointAnimation, "MyAnimatedEllipseGeometry");

这行代码使用 Storyboard.SetTargetName 方法设置了动画的目标对象。在这个例子中,目标对象的名称是 "MyAnimatedEllipseGeometry"。这意味着动画将应用于名为 "MyAnimatedEllipseGeometry" 的对象。

Storyboard.SetTargetProperty(pointAnimation, new PropertyPath(EllipseGeometry.CenterProperty));

这行代码使用 Storyboard.SetTargetProperty 方法设置了动画的目标属性。在这个例子中,目标属性是 EllipseGeometry.CenterProperty,这是一个 PropertyPath 对象。这意味着动画将改变目标对象的 Center 属性。

创建 Storyboard 并添加动画:

storyboard = new Storyboard();
storyboard.Children.Add(pointAnimation);

开始、暂停、继续、停止动画:

  private void Button_Click_Begin(object sender, RoutedEventArgs e)
{
storyboard.Begin(this,true); } private void Button_Click_Pause(object sender, RoutedEventArgs e)
{
storyboard.Pause(this); } private void Button_Click_Resume(object sender, RoutedEventArgs e)
{
storyboard.Resume(this);
} private void Button_Click_Stop(object sender, RoutedEventArgs e)
{
storyboard.Stop(this);
}
 storyboard.Begin(this,true);

Begin方法选择这种重载形式的原因是这样开启的动画是可控制的,如果直接开始动画不受控制,无法暂停、继续、停止。

示例效果

示例效果如下所示:

总结

通过本示例,我们接触了PointAnimationUsingPath类、PathGeometry类和Storyboard类,并使用了它们的一些属性与方法,以后如果遇到沿着特定路径的动画的需求,可以尝试使用这种方法,希望对你有所帮助。

WPF动画教程(PointAnimationUsingPath的使用)的更多相关文章

  1. (推荐)WPF动画教程

    无意间发现博主的文章,记录推荐一下. http://www.cnblogs.com/alamiye010/archive/2009/06/17/1505346.html http://www.cnbl ...

  2. silverlight,WPF动画终极攻略之白云飘,坐车去旅游篇(Blend 4开发)

    原文:silverlight,WPF动画终极攻略之白云飘,坐车去旅游篇(Blend 4开发) 这章有点长,所以我分成了两章.这一章主要是准备工作,差不多算美工篇吧,这章基本不会介绍多少动画效果,主要讲 ...

  3. silverlight,WPF动画终极攻略之迟来的第三章 动画整合篇(Blend 4开发)

    原文:silverlight,WPF动画终极攻略之迟来的第三章 动画整合篇(Blend 4开发) 有个问题想请教下大家,我仿了腾讯的SL版QQ,相似度95%以上.我想写成教程教大家怎么开发出来,会不会 ...

  4. silverlight,WPF动画终极攻略之会飞的小鸟篇(Blend 4开发)

    原文:silverlight,WPF动画终极攻略之会飞的小鸟篇(Blend 4开发) 本教程基本涵盖了WPF和silverlight中的各种动画.先上张效果图. 声明下,这个做的不是让大家照搬的,只是 ...

  5. Expression Design与Blend制作滚动的小球动画教程

    原文:Expression Design与Blend制作滚动的小球动画教程 一,开发工具 Microsoft Expression Design & Blend 4.0 (3.0亦可). 这两 ...

  6. WPF入门教程系列二十三——DataGrid示例(三)

    DataGrid的选择模式 默认情况下,DataGrid 的选择模式为“全行选择”,并且可以同时选择多行(如下图所示),我们可以通过SelectionMode 和SelectionUnit 属性来修改 ...

  7. WPF入门教程系列三——Application介绍(续)

    接上文WPF入门教程系列二——Application介绍,我们继续来学习Application 三.WPF应用程序的关闭 WPF应用程序的关闭只有在应用程序的 Shutdown 方法被调用时,应用程序 ...

  8. WPF入门教程系列二——Application介绍

    一.Application介绍 WPF和WinForm 很相似, WPF与WinForm一样有一个 Application对象来进行一些全局的行为和操作,并且每个 Domain (应用程序域)中仅且只 ...

  9. WPF入门教程系列(二) 深入剖析WPF Binding的使用方法

    WPF入门教程系列(二) 深入剖析WPF Binding的使用方法 同一个对象(特指System.Windows.DependencyObject的子类)的同一种属性(特指DependencyProp ...

  10. WPF入门教程系列(一) 创建你的第一个WPF项目

    WPF入门教程系列(一) 创建你的第一个WPF项目 WPF基础知识 快速学习绝不是从零学起的,良好的基础是快速入手的关键,下面先为大家摞列以下自己总结的学习WPF的几点基础知识: 1) C#基础语法知 ...

随机推荐

  1. Go微服务框架go-kratos实战学习07:consul 作为服务注册和发现中心

    一.Consul 简介 consul 是什么 HashiCorp Consul 是一种服务网络解决方案,它能够管理服务之间以及跨本地和多云环境和运行时的安全网络连接.Consul 它能提供服务发现.服 ...

  2. 项目实战:Qt + 树莓派3B+ 智能笔筒系统

    红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术.树莓派.三维.OpenCV.OpenGL.ffmpeg.OSG.单片机.软硬结合等等)持续更新中-(点击传送门)   需求   1.基于树莓 ...

  3. 使用原生 cookieStore 方法,让 Cookie 操作更简单

    前言 对于前端来讲,我们在操作cookie时往往都是基于document.cookie,但它有一个缺点就是操作复杂,它并没有像localStorage那样提供一些get或set等方法供我们使用.对与c ...

  4. 名校 AI 课程|斯坦福 CS25:Transformers United 专题讲座

    自 2017 年提出后,Transformer 名声大噪,不仅颠覆了自然语言处理(NLP)领域,而且在计算机视觉(CV).强化学习(RL).生成对抗网络(GANs).语音甚至是生物学等领域也大显锋芒, ...

  5. PMP考试计算题汇总

    第6章 项目时间管理 本节术语较多.涉及的工具&技术也不少. 主要包括活动定义.活动排序.活动资源估算.活动历时估算.进度制定.进度控制6个子过程. 1.1活动定义:就是对WBS的进一步分解. ...

  6. 【Azure 应用服务】PHP项目部署到App Service for Linux环境中,如何修改上传文件大小的限制呢?

    问题描述 PHP项目部署到App Service for Linux环境中,如何修改上传文件大小的限制呢? 问题解答 经过查询Azure App Service官方文档,可能通过在项目根目录下添加.h ...

  7. GDB调试入门笔记

    目录 What? Why How 安装GDB 安装命令 查看是否安装成功 调试简单的程序 预备一个程序 调试 使用 break info list next print step 一些小技巧 在gdb ...

  8. Android学习之文件存储

    •前言 任何一个应用程序,其实说白了就是在不停地和数据打交道,我们聊QQ.看新闻.刷微博,所关心的都是里面的数据, 没有数据的应用程序就变成了一个空壳子,对用户来说没有任何实际用途. 那么这些数据都是 ...

  9. offline RL | D4RL:最常用的 offline 数据集之一

    pdf:https://arxiv.org/pdf/2004.07219.pdf html:https://ar5iv.labs.arxiv.org/html/2004.07219 GitHub:ht ...

  10. Prometheus技术分享——如何监控宿主机和容器

    这一期主要来跟大家聊一下,使用node_exporter工具来暴露主机和因公程序上的指标,利用prometheus来监控宿主机:以及通过通过Cadvisor监控docker容器. 一.部署node_e ...