这是它的样子: 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/