/// <summary> /// 缓动缩放动画 (提升效率)Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline), new FrameworkPropertyMetadata { DefaultValue = 20 }); /// </summary> /// <param name="element">缩放控件</param> /// <param name="aTime">缩放时间</param> /// <param name="dFrom">缩放起始值(推荐1)</param> /// <param name="dTo">缩放结束值(推荐1.5)</param> /// <param name="aOscillations">滑过动画目标的次数(推荐5)</param> /// <param name="aSpringiness">弹簧刚度(推荐10)</param> /// <returns>返回动画对象</returns> public static AnimationClock ScaleEasingAnimation(FrameworkElement element, TimeSpan aTime, double dFrom, double dTo, int aOscillations, int aSpringiness) { ScaleTransform scale = new ScaleTransform(); element.RenderTransform = scale; element.RenderTransformOrigin = new Point(0.5, 0.5);//定义圆心位置 EasingFunctionBase easing = new ElasticEase() { EasingMode = EasingMode.EaseOut, //公式 Oscillations = aOscillations, //滑过动画目标的次数 Springiness = aSpringiness //弹簧刚度 }; DoubleAnimation scaleAnimation = new DoubleAnimation() { From = dFrom, //起始值 To = dTo, //结束值 EasingFunction = easing, //缓动函数 Duration = aTime //动画播放时间 }; AnimationClock clock = scaleAnimation.CreateClock(); scale.ApplyAnimationClock(ScaleTransform.ScaleXProperty, clock); scale.ApplyAnimationClock(ScaleTransform.ScaleYProperty, clock); return(clock); }
/// <summary> /// 弹簧式缩小 /// </summary> /// <param name="element"></param> public static void ScaleEasingOutAnimation(this FrameworkElement element) { ScaleTransform scale = new ScaleTransform(); if (element.RenderTransform is TransformGroup) { TransformGroup group = element.RenderTransform as TransformGroup; var item = group.Children.SingleOrDefault(entity => entity is ScaleTransform); group.Children.Remove(item); RotateTransform itemRotate = group.Children.SingleOrDefault(entity => entity is RotateTransform) as RotateTransform; scale.CenterX = itemRotate.CenterX; scale.CenterY = itemRotate.CenterY; group.Children.Add(scale); } else { element.RenderTransform = scale; element.RenderTransformOrigin = new Point(0.5, 0.5); } DoubleAnimation scaleAnimation = new DoubleAnimation() { To = 1, Duration = new TimeSpan(0, 0, 0, 1, 0) }; AnimationClock clock = scaleAnimation.CreateClock(); scale.ApplyAnimationClock(ScaleTransform.ScaleXProperty, clock, HandoffBehavior.Compose); scale.ApplyAnimationClock(ScaleTransform.ScaleYProperty, clock, HandoffBehavior.Compose); }
/// <summary> /// Animate the <see cref="ScaleTransform.ScaleX"/> and <see cref="ScaleTransform.ScaleY"/> /// properties of the main button to a given value. /// </summary> private void ChangeScale(double to) { DoubleAnimation anim = new DoubleAnimation(ButtonScaleTransform.ScaleX, to, new Duration(TimeSpan.FromMilliseconds(500 * to))) { EasingFunction = new ExponentialEase() }; AnimationClock clock = anim.CreateClock(); ButtonScaleTransform.ApplyAnimationClock(ScaleTransform.ScaleXProperty, clock); ButtonScaleTransform.ApplyAnimationClock(ScaleTransform.ScaleYProperty, clock); }
private void scaleOutAnim_Completed(object sender, EventArgs e) { _switchIn.ApplyAnimationClock(FrameworkElement.OpacityProperty, null); _switchIn.Visibility = Visibility.Hidden; ScaleTransform scaleTransform = (ScaleTransform)GetRenderTransform(_switchIn, typeof(ScaleTransform)); scaleTransform.ApplyAnimationClock(ScaleTransform.ScaleXProperty, null); scaleTransform.ApplyAnimationClock(ScaleTransform.ScaleYProperty, null); scaleTransform.ScaleX = scaleTransform.ScaleY = 1; AnimationCompletedEvent(new EventArgs()); }
private void scaleInAnim_Completed(object sender, EventArgs e) { _switchIn.ApplyAnimationClock(FrameworkElement.OpacityProperty, null); ScaleTransform scaleTransform = (ScaleTransform)GetRenderTransform(_switchIn, typeof(ScaleTransform)); scaleTransform.ApplyAnimationClock(ScaleTransform.ScaleXProperty, null); scaleTransform.ApplyAnimationClock(ScaleTransform.ScaleYProperty, null); scaleTransform.ScaleX = scaleTransform.ScaleY = 1; _switchIn.Opacity = 1; _switchIn.IsHitTestVisible = true; AnimationCompletedEvent(e); }
/// <summary> /// 弹簧式放大 /// </summary> /// <param name="element"></param> public static void ScaleEasingInAnimation(this FrameworkElement element, bool isActivated = true) { ScaleTransform scale = new ScaleTransform(); if (element.RenderTransform is TransformGroup) { TransformGroup group = element.RenderTransform as TransformGroup; var item = group.Children.SingleOrDefault(entity => entity is ScaleTransform); if (item != null) { group.Children.Remove(item); } RotateTransform itemRotate = group.Children.SingleOrDefault(entity => entity is RotateTransform) as RotateTransform; scale.CenterX = itemRotate.CenterX; scale.CenterY = itemRotate.CenterY; group.Children.Add(scale); } else { element.RenderTransform = scale; element.RenderTransformOrigin = new Point(0.5, 0.5); } if (!isActivated) { scale.ScaleX = 1.2; scale.ScaleY = 1.2; return; } EasingFunctionBase easing = new ElasticEase() { EasingMode = EasingMode.EaseOut, Oscillations = 10, Springiness = 15 }; DoubleAnimation scaleAnimation = new DoubleAnimation() { //From = 1, To = 1.2, EasingFunction = easing, Duration = new TimeSpan(0, 0, 0, 1, 500), FillBehavior = FillBehavior.HoldEnd }; AnimationClock clock = scaleAnimation.CreateClock(); scale.ApplyAnimationClock(ScaleTransform.ScaleXProperty, clock, HandoffBehavior.Compose); scale.ApplyAnimationClock(ScaleTransform.ScaleYProperty, clock, HandoffBehavior.Compose); }
private void item_Expanded(object sender, RoutedEventArgs e) { if (!Settings.AnimationsEnabled) { ScaleTransform transform = (sender as TreeViewItem).Template.FindName("itemsHostScale", sender as TreeViewItem) as ScaleTransform; if (transform != null) { transform.ApplyAnimationClock(ScaleTransform.ScaleYProperty, null); } } }
/// <summary> /// 缩放对象 /// </summary> /// <param name="target"></param> /// <param name="max"></param> /// <param name="durationMilliseconds"></param> /// <param name="easingMode"></param> public static void Scale(UIElement target, double from, double to, double durationMilliseconds, EasingMode easingMode) { ScaleTransform scale = new ScaleTransform(); target.RenderTransform = scale; target.RenderTransformOrigin = new Point(0.5, 0.5); EasingFunctionBase easeFunction = new PowerEase() { EasingMode = easingMode, Power = EasingPower }; DoubleAnimation animation = new DoubleAnimation() { From = from, //起始值 To = to, //结束值 EasingFunction = easeFunction, //缓动函数 Duration = TimeSpan.FromMilliseconds(durationMilliseconds) //动画播放时间 }; AnimationClock clock = animation.CreateClock(); scale.ApplyAnimationClock(ScaleTransform.ScaleXProperty, clock); scale.ApplyAnimationClock(ScaleTransform.ScaleYProperty, clock); }
private void scaleOut2Anim_Completed(object sender, EventArgs e) { QuarticEase Ease = new QuarticEase(); Ease.EasingMode = EasingMode.EaseOut; _switchOut.Visibility = Visibility.Collapsed; _switchOut.ApplyAnimationClock(FrameworkElement.OpacityProperty, null); ScaleTransform scaleOut = (ScaleTransform)GetRenderTransform(_switchOut, typeof(ScaleTransform)); scaleOut.ApplyAnimationClock(ScaleTransform.ScaleXProperty, null); scaleOut.ApplyAnimationClock(ScaleTransform.ScaleYProperty, null); ScaleTransform scaleIn = (ScaleTransform)GetRenderTransform(_switchIn, typeof(ScaleTransform)); scaleIn.CenterX = centerX; scaleIn.CenterY = centerY; scaleIn.ScaleX = scaleIn.ScaleY = 1.1; DoubleAnimation scaleInAnim = new DoubleAnimation(1, AnimationDuration, FillBehavior.Stop); scaleInAnim.Completed += scaleInAnim_Completed; scaleInAnim.EasingFunction = Ease; _switchIn.Visibility = Visibility.Visible; DoubleAnimation opacityInAnim = new DoubleAnimation(0, 1, AnimationDuration, FillBehavior.Stop); // To do, or not to do opacityInAnim.EasingFunction = Ease; scaleIn.BeginAnimation(ScaleTransform.ScaleXProperty, scaleInAnim); scaleIn.BeginAnimation(ScaleTransform.ScaleYProperty, scaleInAnim); _switchIn.BeginAnimation(FrameworkElement.OpacityProperty, opacityInAnim); }
// Create and apply and animation when the button is clicked. private void myButton_Clicked(object sender, RoutedEventArgs e) { // Create a DoubleAnimation to animate the // ScaleTransform. DoubleAnimation myAnimation = new DoubleAnimation( 1, // "From" value 5, // "To" value new Duration(TimeSpan.FromSeconds(5)) ); myAnimation.AutoReverse = true; // Create a clock the for the animation. AnimationClock myClock = myAnimation.CreateClock(); // Associate the clock the ScaleX and // ScaleY properties of the button's // ScaleTransform. myScaleTransform.ApplyAnimationClock( ScaleTransform.ScaleXProperty, myClock); myScaleTransform.ApplyAnimationClock( ScaleTransform.ScaleYProperty, myClock); }
/// <summary> /// 缓动缩放动画 /// </summary> /// <param name="element">控件名</param> /// <param name="from">元素开始的大小</param> /// <param name="to">元素到达的大小</param> /// <param name="time">持续时间(毫秒)</param> public static void ScaleEasingAnimationShow(FrameworkElement element, double from, double to, int time) { lock (element) { ScaleTransform scale = new ScaleTransform(); element.RenderTransform = scale; element.RenderTransformOrigin = new Point(0.5, 0.5);//定义圆心位置 EasingFunctionBase easeFunction = new PowerEase() { EasingMode = EasingMode.EaseOut, Power = 5 }; DoubleAnimation scaleAnimation = new DoubleAnimation() { From = from, //起始值 To = to, //结束值 EasingFunction = easeFunction, //缓动函数 Duration = new TimeSpan(0, 0, 0, 0, time) //动画播放时间 }; AnimationClock clock = scaleAnimation.CreateClock(); scale.ApplyAnimationClock(ScaleTransform.ScaleXProperty, clock); scale.ApplyAnimationClock(ScaleTransform.ScaleYProperty, clock); } }
public static void ScaleEasingAnimation(FrameworkElement element) { ScaleTransform scale = new ScaleTransform(); element.RenderTransform = scale; element.RenderTransformOrigin = new Point(0.5, 0.5);//定义圆心位置 EasingFunctionBase easing = new ElasticEase() { EasingMode = EasingMode.EaseOut, //公式 Oscillations = 1, //滑过动画目标的次数 Springiness = 10 //弹簧刚度 }; DoubleAnimation scaleAnimation = new DoubleAnimation() { From = 0, //起始值 To = 1, //结束值 EasingFunction = easing, //缓动函数 Duration = new TimeSpan(0, 0, 0, 1, 200) //动画播放时间 }; AnimationClock clock = scaleAnimation.CreateClock(); scale.ApplyAnimationClock(ScaleTransform.ScaleXProperty, clock); scale.ApplyAnimationClock(ScaleTransform.ScaleYProperty, clock); }