当前位置: 首页 > news >正文

wpf 路径动画 举例

先,我们需要在XAML中定义一个Path,这个Path将定义动画的路线。然后,我们将使用DoubleAnimationUsingPath来沿着这个路径移动一个元素(比如一个矩形)。

<Window x:Class="WpfApp.MainWindow"  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  Title="Path Animation Example" Height="350" Width="525">  <Grid>  <Canvas Name="myCanvas">  <!-- 定义路径 -->  <Path x:Name="myPath" Stroke="Black" StrokeThickness="2" Data="M 10,100 C 100,10 200,200 300,100"/><!-- 定义一个矩形,我们将对其进行动画处理 -->  <Rectangle Name="myRectangle" Width="20" Height="20" Fill="Red" Canvas.Left="10" Canvas.Top="100">  <Rectangle.RenderTransform>  <TranslateTransform x:Name="rectTransform" X="0" Y="0"/>  </Rectangle.RenderTransform>  <!-- 定义动画 -->  <Rectangle.Triggers>  <EventTrigger RoutedEvent="Loaded">  <BeginStoryboard>  <Storyboard>  <!-- 路径动画 -->  <DoubleAnimationUsingPath  Storyboard.TargetName="rectTransform"  Storyboard.TargetProperty="Y"  PathGeometry="{Binding Data, ElementName=myPath}"  Duration="0:0:5"  RepeatBehavior="Forever"  AutoReverse="True"/>  </Storyboard>  </BeginStoryboard>  </EventTrigger>  </Rectangle.Triggers>  </Rectangle>  <!-- 注意:为了简化示例,我没有直接在XAML中给Path命名,但在实际使用中你可能需要这样做来绑定 -->  <!-- <Path x:Name="myPath" Stroke="Black" StrokeThickness="2" Data="M 10,100 C 100,10 200,200 300,100"/> -->  </Canvas>  </Grid>  
</Window>

  1. 在代码后台(MainWindow.xaml.cs)中,设置动画并绑定到PathData
    public MainWindow()  
    {  InitializeComponent();  DoubleAnimationUsingPath pathAnimation = new DoubleAnimationUsingPath  {  Duration = TimeSpan.FromSeconds(5),  RepeatBehavior = RepeatBehavior.Forever,  AutoReverse = true,  PathGeometry = myPath.Data.GetFlattenedPathGeometry()  };  Storyboard.SetTargetName(pathAnimation, "rectTransform");  Storyboard.SetTargetProperty(pathAnimation, new PropertyPath("Y"));

http://www.lryc.cn/news/424399.html

相关文章:

  • 【C++】classes and object 2.8 取地址及const取地址操作符重载
  • milvus helm k8s开启监控
  • 牛奶饮用学习笔记
  • php防止页面重复刷新或者重复提交
  • Springboot3 配置sql打印到控制台
  • 深入理解 GO 语言并发
  • leetcode39组合总和
  • 【JPCS独立出版,EI稳定检索】2024年工业机器人与先进制造技术国际学术会议(IRAMT 2024,9月27-29)
  • Fal.ai Flux 1-Pro/Viva.ai/哩布哩布AI:AI绘图部分免费工具+原图提示词Prompt
  • C++学习笔记----2、使用C++进行优雅编程(十)---- 格式化
  • 双指针| Java | (hot100) 力扣283, 11, 15, 42做题总结
  • matlab求解方程
  • MySQL基础--视图,存储过程
  • 学习记录第二十六天
  • Polars简明基础教程十一:可视化(一)
  • 实战项目:贪吃蛇游戏的实现(上)
  • SHT30温湿度传感器全解析——概况,性能,MCU连接,样例代码
  • SQL server 同环比计算模板
  • python发送外部请求
  • c++并发编程面试题
  • K8S上安装LongHorn(分布式块存储) --use
  • 2024年前端技术发展趋势分析
  • spring boot 笔记大杂烩
  • 如何在香港云服务器上优化网站性能?
  • STM32低功耗与备用备份区域
  • 武汉某汽配公司携手三品软件 共绘PLM项目新蓝图
  • uniapp多图上传uni.chooseImage上传照片uni.uploadFile,默认上传9张图
  • MySQL——内置函数
  • 2024年最新版小程序云开发数据模型的开通步骤,支持可视化数据库管理,支持Mysql和NoSql数据库,可以在vue3前端web里调用操作
  • 智慧水库大坝安全监测预警系统解决方案