使用Blend制作PathAnimation
1:选中Path转换为运动路径
2:选择目标对象
PathAnimation使用动态的Path
PathAnimation动画在播放的时候,PahtGeometry是已经确定的,不会改变,不会实时的根据Path的改变动画进行调整,所以是不能进行绑定
只有在每一次需要播放动画之前读取相关的Path的Data,读取当前的PathGeometry然后开始动画
1 <Path x:Name="path" Stroke="DarkGray" StrokeThickness="3"> 2 <Path.Data> 3 <PathGeometry> 4 <PathFigure 5 StartPoint="{Binding Center, ElementName=ptStart}"> 6 <BezierSegment 7 Point1="{Binding Center, ElementName=ptCtrl1}" 8 Point2="{Binding Center, ElementName=ptCtrl2}" 9 Point3="{Binding Center, ElementName=ptEnd}"/> 10 </PathFigure> 11 </PathGeometry> 12 </Path.Data> 13 </Path>
1 private void Button_Click(object sender, RoutedEventArgs e) 2 { 3 Canvas.SetLeft(this.truck, -this.truck.ActualWidth / 2); 4 Canvas.SetTop(this.truck, -this.truck.ActualHeight / 2); 5 this.truck.RenderTransformOrigin = new Point(0.5, 0.5); 6 7 MatrixTransform matrix = new MatrixTransform(); 8 this.truck.RenderTransform = matrix; 9 10 NameScope.SetNameScope(this, new NameScope()); 11 this.RegisterName("matrix", matrix); 12 13 MatrixAnimationUsingPath matrixAnimation = new MatrixAnimationUsingPath(); 14 matrixAnimation.PathGeometry = this.path.Data.GetFlattenedPathGeometry(); 15 matrixAnimation.Duration = new Duration(TimeSpan.FromSeconds(5)); 16 17 //matrixAnimation.AutoReverse = true; //重复 18 //matrixAnimation.IsOffsetCumulative = !matrixAnimation.AutoReverse; 19 //matrixAnimation.RepeatBehavior = RepeatBehavior.Forever; 20 matrixAnimation.DoesRotateWithTangent = false; //旋转 21 22 Storyboard story = new Storyboard(); 23 story.Children.Add(matrixAnimation); 24 Storyboard.SetTargetName(matrixAnimation, "matrix"); 25 Storyboard.SetTargetProperty(matrixAnimation, new PropertyPath(MatrixTransform.MatrixProperty)); 26 story.Begin(this); 27 }