WPF路径动画 [英] WPF path animation

查看:342
本文介绍了WPF路径动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了下面的XAML

I have created the following 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.所有我想要做的路径是沿着节点1节点2对路径动画的对象。

As you can see, I have created 2 elliptical Nodes. A path connecting the two nodes and an object sitting at node 1. All I want to do here is to animate the object at node1 along the path towards node2.

我试图做使用code ,因为我希望动画在node2上的点击发生的动画。我一直在挣扎DoubleAnimation是,MatrixAnimation,storyboard..Very混乱。请分享你如何实现这方面的知识。我希望同样的code将与曲线和复杂的路径工作。

I am trying to do the animation using code as I want the animation to happen on click of the node2. I have been struggling with DoubleAnimation, MatrixAnimation,storyboard..Very confusing. Please share you knowledge on how to achieve this. I am hoping the same code would work with curves and complex paths.

推荐答案

您需要 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>

然后再从code调用:

Then to invoke from code:

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



我想,你在这里丢失了关键的工具:混合。作为在code创建动画,如果你看看XAML元素,想想他们作为序列化类,它可以在code,即得到体现。


I think you're missing critical tool here: Blend. As for creating animation in code, if you look at XAML elements, think about them as serialized classes, which can be reflected in code, i.e.

    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)很痛苦的创建者直接从code。这一切真的取决于应用程序。
看看这里共混物的起点。

这篇关于WPF路径动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆