public void Animate( IAnimationContext context, System.Windows.Controls.Control control, double x, double y, TimeSpan duration ) { if ( !double.IsNaN( x ) ) { double from = GraphCanvas.GetX( control ); from = double.IsNaN( from ) ? 0.0 : from; //create the animation for the horizontal position var animationX = new DoubleAnimation( from, x, duration, FillBehavior.HoldEnd ); animationX.Completed += ( s, e ) => { control.BeginAnimation( GraphCanvas.XProperty, null ); control.SetValue( GraphCanvas.XProperty, x ); }; control.BeginAnimation( GraphCanvas.XProperty, animationX, HandoffBehavior.Compose ); } if ( !double.IsNaN( y ) ) { double from = GraphCanvas.GetY( control ); from = ( double.IsNaN( from ) ? 0.0 : from ); //create an animation for the vertical position var animationY = new DoubleAnimation( from, y, duration, FillBehavior.HoldEnd ); animationY.Completed += ( s, e ) => { control.BeginAnimation( GraphCanvas.YProperty, null ); control.SetValue( GraphCanvas.YProperty, y ); }; control.BeginAnimation( GraphCanvas.YProperty, animationY, HandoffBehavior.Compose ); } }
/// <summary> /// 播放动画() /// </summary> /// <param name="from">From</param> /// <param name="to">To</param> /// <param name="dp">依赖属性</param> /// <param name="target">对象</param> /// <param name="duration">时长</param> public static void PlayAnimation(double from, double to, DependencyProperty dp, System.Windows.Shapes.Shape target, double duration = 1) { DoubleAnimation animation = new DoubleAnimation() { From = from, To = to, Duration = TimeSpan.FromSeconds(duration), FillBehavior = FillBehavior.HoldEnd, AccelerationRatio = .5, EasingFunction = CAAnimation.be }; target.BeginAnimation(dp, animation); }