protected sealed override void UpdateDots() { if (DotCount < 1) { return; } PrivateCanvas.Children.Clear(); //计算相关尺寸 var centerWidth = DotDiameter * DotCount + DotInterval * (DotCount - 1) + MoveLength; var speedDownLength = (ActualWidth - MoveLength) / 2; var speedUniformLength = centerWidth / 2; //定义动画 Storyboard = new Storyboard { RepeatBehavior = RepeatBehavior.Forever }; //创建圆点 for (var i = 0; i < DotCount; i++) { var ellipse = CreateEllipse(); var frames = new ThicknessAnimationUsingKeyFrames { BeginTime = TimeSpan.FromMilliseconds(DotDelayTime * i) }; //开始位置 var frame0 = new LinearThicknessKeyFrame { Value = new Thickness(ellipse.Margin.Left, 0, 0, 0), KeyTime = KeyTime.FromTimeSpan(TimeSpan.Zero) }; //开始位置到匀速开始 var frame1 = new EasingThicknessKeyFrame { EasingFunction = new PowerEase { EasingMode = EasingMode.EaseOut }, Value = new Thickness(speedDownLength + ellipse.Margin.Left, 0, 0, 0), KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(DotSpeed * (1 - UniformScale) / 2)) }; //匀速开始到匀速结束 var frame2 = new LinearThicknessKeyFrame { Value = new Thickness(speedDownLength + speedUniformLength + ellipse.Margin.Left, 0, 0, 0), KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(DotSpeed * (1 + UniformScale) / 2)) }; //匀速结束到匀加速结束 var frame3 = new EasingThicknessKeyFrame { EasingFunction = new PowerEase { EasingMode = EasingMode.EaseIn }, Value = new Thickness(ActualWidth + ellipse.Margin.Left + speedUniformLength, 0, 0, 0), KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(DotSpeed)) }; frames.KeyFrames.Add(frame0); frames.KeyFrames.Add(frame1); frames.KeyFrames.Add(frame2); frames.KeyFrames.Add(frame3); Storyboard.SetTarget(frames, ellipse); Storyboard.SetTargetProperty(frames, new PropertyPath(MarginProperty)); Storyboard.Children.Add(frames); PrivateCanvas.Children.Add(ellipse); } Storyboard.Begin(); }
public ShakeShakeAnimation(Thickness original) { originalThickness = original; const double amplitude = 0.02; double tLeft, tRight; tLeft = original.Left; tRight = original.Right; double shakeQuantity = Math.Min(tLeft, tRight); ThicknessKeyFrame kf1 = new LinearThicknessKeyFrame() { Value = new Thickness() { Left = originalThickness.Left - shakeQuantity, Top = originalThickness.Top, Right = originalThickness.Right + shakeQuantity, Bottom = originalThickness.Bottom }, KeyTime = KeyTime.FromPercent(1 * amplitude) }; ThicknessKeyFrame kf2 = new LinearThicknessKeyFrame() { Value = new Thickness() { Left = originalThickness.Left + shakeQuantity, Top = originalThickness.Top, Right = originalThickness.Right - shakeQuantity, Bottom = originalThickness.Bottom }, KeyTime = KeyTime.FromPercent(3 * amplitude) }; ThicknessKeyFrame kf3 = new LinearThicknessKeyFrame() { Value = new Thickness() { Left = originalThickness.Left - shakeQuantity, Top = originalThickness.Top, Right = originalThickness.Right + shakeQuantity, Bottom = originalThickness.Bottom }, KeyTime = KeyTime.FromPercent(5 * amplitude) }; ThicknessKeyFrame kf4 = new LinearThicknessKeyFrame() { Value = new Thickness() { Left = originalThickness.Left, Top = originalThickness.Top, Right = originalThickness.Right, Bottom = originalThickness.Bottom }, KeyTime = KeyTime.FromPercent(6 * amplitude) }; Duration = new Duration(new TimeSpan(0, 0, 2)); KeyFrames.Add(kf1); KeyFrames.Add(kf2); KeyFrames.Add(kf3); KeyFrames.Add(kf4); RepeatBehavior = RepeatBehavior.Forever; }