【发布时间】:2012-12-11 16:31:39
【问题描述】:
这是它的样子: http://screencast.com/t/4cd1yQJReXt
假设小船图像遵循您为它绘制的路径。它确实有效 - 它遵循路径,但它从错误的地方开始。我无法弄清楚我的动画出了什么问题,我现在已经多次查看代码。应用程序的 XAML 非常简单,所有元素都包含在其中 画布名称="Canvas1" 背景="黑色"
这个画布是一个子元素 窗口
下面是执行动画的代码。我知道我喂它的线是正确的。只是他们的参考点有点乱。应该是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);
谢谢!
【问题讨论】:
标签: wpf animation path geometry transformation