gpt4 book ai didi

c# - WPF路径动画

转载 作者:太空狗 更新时间:2023-10-29 23:40:05 37 4
gpt4 key购买 nike

我创建了以下 XAML

<Canvas Background="Gray" Margin="10">    
<Ellipse x:Name="Node1" Width="20" Height="20" Fill="AliceBlue" Canvas.Left="38" Canvas.Top="136" />
<Ellipse x:Name="Node2" Width="20" Height="20" Fill="AliceBlue" Canvas.Left="290" Canvas.Top="136" />
<Ellipse x:Name="object" Width="10" Height="20" Fill="Black" Canvas.Left="43" Canvas.Top="125" />
<Path Stroke="Black" StrokeThickness="1">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigureCollection>
<PathFigure StartPoint="50,145">
<PathFigure.Segments>
<PathSegmentCollection>
<LineSegment Point="100,100" />
<LineSegment Point="250,100" />
<LineSegment Point="300,145" />
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
</PathFigureCollection>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
</Canvas>

如您所见,我创建了 2 个椭圆节点。一条连接两个节点的路径和一个位于节点 1 的对象。我在这里要做的就是沿着通往节点 2 的路径为节点 1 处的对象设置动画。

我正在尝试使用代码制作动画,因为我希望动画在单击 node2 时发生。我一直在努力处理 DoubleAnimation、MatrixAnimation、 Storyboard……非常困惑。请分享您关于如何实现这一目标的知识。我希望相同的代码适用于曲线和复杂路径。

最佳答案

您需要DoubleAnimationUsingPath (ref) :

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication1.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Window.Resources>
<Storyboard x:Key="Storyboard1">
<DoubleAnimationUsingPath Duration="0:0:2" Source="X" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="object">
<DoubleAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M2,10 L52,-35 L202,-35 L252,10"/>
</DoubleAnimationUsingPath.PathGeometry>
</DoubleAnimationUsingPath>
<DoubleAnimationUsingPath Duration="0:0:2" Source="Y" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="object">
<DoubleAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M2,10 L52,-35 L202,-35 L252,10"/>
</DoubleAnimationUsingPath.PathGeometry>
</DoubleAnimationUsingPath>
</Storyboard>
</Window.Resources>

<Grid x:Name="LayoutRoot">
<Canvas Background="Gray" Margin="10">
<Ellipse x:Name="Node1" Width="20" Height="20" Fill="AliceBlue" Canvas.Left="38" Canvas.Top="136" />
<Ellipse x:Name="Node2" Width="20" Height="20" Fill="AliceBlue" Canvas.Left="290" Canvas.Top="136" />
<Ellipse x:Name="object" Width="10" Height="20" Fill="Black" Canvas.Left="43" Canvas.Top="125" RenderTransformOrigin="0.5,0.5" MouseLeftButtonDown="object_MouseLeftButtonDown" >
<Ellipse.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Ellipse.RenderTransform>
</Ellipse>
<Path Stroke="Black" StrokeThickness="1">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigureCollection>
<PathFigure StartPoint="50,145">
<PathFigure.Segments>
<PathSegmentCollection>
<LineSegment Point="100,100" />
<LineSegment Point="250,100" />
<LineSegment Point="300,145" />
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
</PathFigureCollection>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
</Canvas>
</Grid>
</Window>

然后从代码调用:

private void object_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Storyboard animation = this.TryFindResource("Storyboard1") as Storyboard;
animation.Begin();
}

[编辑]我认为您在这里缺少关键工具:Blend .至于在代码中创建动画,如果您查看 XAML 元素,请将它们视为序列化的类,它们可以反射(reflect)在代码中,即

    Storyboard sb = new Storyboard();
DoubleAnimationUsingPath ani_2 = new DoubleAnimationUsingPath();
ani_2.Duration = new Duration(new TimeSpan(0, 0, 2));

PathGeometry pg = new PathGeometry();
pg.Figures.Add(new PathFigure());

ani_2.PathGeometry.AddGeometry(pg);

等然而(IMAO)直接从代码创建这些是相当痛苦的。这完全取决于应用程序。看看here Blend 的起点。

关于c# - WPF路径动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14020798/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com