动画AutoReverse = false但无论如何都是从错误的起点开始

时间:2014-06-17 17:28:41

标签: c# .net wpf animation .net-4.5

我试图从画布的右下角到画布的左上角获取图像,并永远重复这种行为。图像不应该从左上角到右上角执行反向动画。我是动画新手,所以我使用了this example

目前我的图像从左上角移动到右下角并返回。我尝试在画布中更改图像的起始位置,结果是动画将此位置用作新的起点。我也尝试使用负值,使图像向相反方向移动。减少段中的点数让我得到了更短的动画路径,但没有别的。我还设置了AutoReverse = false,动画行为没有任何变化。

我的想法是,   - 段类正在构建一个圆点,但是要使用哪个其他类?   - 开始位置必须改变,但是如何让对象向上移动而不是向下移动?

我的代码,

Storyboard animationSB = new Storyboard();

//Image book = createImage(model.keywordCollection[0].cover.small);
Image rope1 = createImage("pack://application:,,,/GUI;component/Resources/rope_trans.png");
rope1.Height = 360.0;
rope1.Width = 185.0;

//Transform to move the book image
TranslateTransform aniRope1 = new TranslateTransform();
this.RegisterName("AnimatedRope1", aniRope1);
rope1.RenderTransform = aniRope1;
Canvas.SetLeft(rope1, 258.659);
Canvas.SetTop(rope1, 583.212);
LeftRope.Children.Add(rope1);


//Anitmation path
PathGeometry animationPath1 = new PathGeometry();
PathFigure pathFigure1 = new PathFigure();
PolyLineSegment lineSegments1 = new PolyLineSegment();
lineSegments1.Points.Add(new Point(LeftRope.ActualWidth, LeftRope.ActualHeight));
lineSegments1.Points.Add(new Point(258.659, 583.212));
lineSegments1.Points.Add(new Point(120.596, 272.665));
lineSegments1.Points.Add(new Point(0, 0));
pathFigure1.Segments.Add(lineSegments1);
animationPath1.Figures.Add(pathFigure1);
animationPath1.Freeze();

//Animate transform to move image along the path on the x-axis
DoubleAnimationUsingPath translateXAnimation1 = new DoubleAnimationUsingPath();
translateXAnimation1.PathGeometry = animationPath1;
translateXAnimation1.Duration = TimeSpan.FromSeconds(6);
translateXAnimation1.Source = PathAnimationSource.X;
translateXAnimation1.AutoReverse = false;

Storyboard.SetTargetName(translateXAnimation1, "AnimatedRope1");
Storyboard.SetTargetProperty(translateXAnimation1, new PropertyPath(TranslateTransform.XProperty));

//Animate transform to move image along the path on the y-axis
DoubleAnimationUsingPath translateYAnimation1 = new DoubleAnimationUsingPath();
translateYAnimation1.PathGeometry = animationPath1;
translateYAnimation1.Duration = TimeSpan.FromSeconds(6);
translateYAnimation1.Source = PathAnimationSource.Y;
translateYAnimation1.AutoReverse = false;

Storyboard.SetTargetName(translateYAnimation1, "AnimatedRope1");
Storyboard.SetTargetProperty(translateYAnimation1, new PropertyPath(TranslateTransform.YProperty));

//Create Storyboard containing and applying the animation

//animationSB.RepeatBehavior = RepeatBehavior.Forever;
animationSB.Children.Add(translateXAnimation1);
animationSB.Children.Add(translateYAnimation1);

animationSB.AutoReverse = false;

故事板以另一种方法启动。

我正在使用.Net 4.5.1 C#桌面应用程序在Windows 8.1N上进行开发。

1 个答案:

答案 0 :(得分:1)

您应该替换以下行:

lineSegments1.Points.Add(new Point(LeftRope.ActualWidth, LeftRope.ActualHeight));

pathFigure1.StartPoint = new Point(LeftRope.ActualWidth, LeftRope.ActualHeight);

事实证明,如果你没有指定StartPoint for PathFigure,它会自动关闭数字,即连接点集合的最后一个和第一个点。