Skip to content

WPF 路径动画

Published: at 14:28

需求

指定对象(Image)按照贝塞尔曲线进行移动

实现

Image

<Image Source="{StaticResource Image.Activity.Score}"
x:Name="StarImage"
VerticalAlignment="Center"
Width="48" Height="51" Stretch="Uniform">
<Image.RenderTransform>
<TranslateTransform x:Name="ScoreImageTranslateTransform"></TranslateTransform>
</Image.RenderTransform>
</Image>

路径与动画定义

<FrameworkElement.Resources>
<ResourceDictionary>
<PathGeometry x:Key="LeftStarAnimationPathGeometry">
<PathFigure StartPoint="0,0">
<BezierSegment Point1="-50,80" Point2="-120,100" Point3="-300,55">
</BezierSegment>
<BezierSegment Point1="-740,-100" Point2="-720,-350" Point3="-740,-452">
</BezierSegment>
</PathFigure>
</PathGeometry>
<Storyboard x:Key="StarMoveUpStoryboardLeft" TargetName="ContentGrid">
<DoubleAnimationUsingPath
BeginTime="00:00:0.00" Duration="00:00:00.30"
Storyboard.TargetName="ScoreImageTranslateTransform"
Storyboard.TargetProperty="X"
Source="X"
PathGeometry="{StaticResource LeftStarAnimationPathGeometry}">
</DoubleAnimationUsingPath>
<DoubleAnimationUsingPath
BeginTime="00:00:0.00" Duration="00:00:00.30"
Storyboard.TargetName="ScoreImageTranslateTransform"
Storyboard.TargetProperty="Y"
Source="Y"
PathGeometry="{StaticResource LeftStarAnimationPathGeometry}">
</DoubleAnimationUsingPath>
</Storyboard>
</ResourceDictionary>
</FrameworkElement.Resources>

参考


原文链接: https://blog.jgrass.cc/posts/wpf-path-animation/

本作品采用 「署名 4.0 国际」 许可协议进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。