WPF 动画遵循正确的路径,但它在空间中关闭

标签 wpf animation path geometry transformation

这是它的样子: http://screencast.com/t/4cd1yQJReXt

小船图像应该遵循您为它绘制的路径。它确实有效 - 它遵循路径但它从错误的地方开始。我无法弄清楚我的动画有什么问题,我已经多次检查代码了。应用程序的 XAML 非常简单,所有元素都包含在其中 Canvas 名称="Canvas1"背景="黑色"

并且这个 Canvas 是 窗口

下面是执行动画的代码。我知道我输入的线是正确的。只是他们的引用点不知何故搞砸了。它应该是 Canvas1 但不是。

            NameScope.SetNameScope(this, new NameScope());

            MatrixTransform buttonMatrixTransform = new MatrixTransform();
            ship1.RenderTransform = buttonMatrixTransform;

            this.RegisterName("ButtonMatrixTransform", buttonMatrixTransform);

            PathGeometry animationPath = new PathGeometry();
            PathFigure pFigure = new PathFigure();
            pFigure.StartPoint = new Point(pathnodes.ElementAt<Line>(0).X1, pathnodes.ElementAt<Line>(0).Y1);

            PolyLineSegment ls = new PolyLineSegment();

            foreach (Line l in pathnodes)
            {
                ls.Points.Add(new Point(l.X1, l.Y1));
                ls.Points.Add(new Point(l.X2, l.Y2));
            }

            pFigure.Segments.Add(ls);      
            animationPath.Figures.Add(pFigure);

            animationPath.Freeze();

            MatrixAnimationUsingPath matrixAnimation =
                new MatrixAnimationUsingPath();

            matrixAnimation.IsOffsetCumulative = false;

            matrixAnimation.PathGeometry = animationPath;
            matrixAnimation.Duration = TimeSpan.FromSeconds(5);
            matrixAnimation.DoesRotateWithTangent = true;

            Storyboard.SetTargetName(matrixAnimation, "ButtonMatrixTransform");
            Storyboard.SetTargetProperty(matrixAnimation,
                new PropertyPath(MatrixTransform.MatrixProperty));

            Storyboard pathAnimationStoryboard = new Storyboard();
            pathAnimationStoryboard.Children.Add(matrixAnimation);

            pathAnimationStoryboard.Begin(this);

谢谢!

最佳答案

我无法观看您的视频 :O( 但是,这可能与未设置 RenderTransformOrigin 有关吗?通常当我进行转换时,我将 RTO 设置为 0.5、0.5,以便原点位于中心对象的。

关于WPF 动画遵循正确的路径,但它在空间中关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14035116/

相关文章:

javascript - 制作硬币翻转动画两侧

ios - 来自 View Controller 的带有提示的 UINavigationController 动画

html - SVG 中的中心路径

c# - 如何将工具提示添加到数据网格单元格

wpf - 如何在 WPF 中的 TextBlock 内为文本添加边框?

javascript - wpf 网页浏览器 javascript 回调

c# - 在 WPF 中更改 BitMapImage 的尺寸,我可以在 <Image> 元素中放置什么样的对象?

CSS3 动画不起作用

Python 路径 : Reusing Python module

java - 解决 Windows 上的 com.apple 导入错误