Files
Shrlalgo.RvKits/WPFluent/Animations/TransitionAnimationProvider.cs

183 lines
6.0 KiB
C#
Raw Normal View History

2025-05-05 17:04:06 +08:00

using System.Windows.Media.Animation;
2025-05-05 17:04:06 +08:00
using WPFluent.Hardware;
namespace WPFluent.Animations;
/// <summary>
2025-05-05 17:04:06 +08:00
/// 提供以下工具 <see cref="FrameworkElement"/> animation.
/// </summary>
/// <example>
/// <code lang="csharp">TransitionAnimationProvider.ApplyTransition(MyFrameworkElement, Transition.FadeIn, 500);</code>
/// </example>
public static class TransitionAnimationProvider
{
private const double DecelerationRatio = 0.7D;
private static void FadeInTransition(UIElement animatedUiElement, Duration duration)
{
var opacityDoubleAnimation = new DoubleAnimation
{
Duration = duration,
DecelerationRatio = DecelerationRatio,
From = 0.0,
To = 1.0,
};
animatedUiElement.BeginAnimation(UIElement.OpacityProperty, opacityDoubleAnimation);
}
private static void FadeInWithSlideTransition(UIElement animatedUiElement, Duration duration)
{
var translateDoubleAnimation = new DoubleAnimation
{
Duration = duration,
DecelerationRatio = DecelerationRatio,
From = 30,
To = 0,
};
2025-05-05 17:04:06 +08:00
if (animatedUiElement.RenderTransform is not TranslateTransform)
{
animatedUiElement.SetCurrentValue(UIElement.RenderTransformProperty, new TranslateTransform(0, 0));
}
2025-05-05 17:04:06 +08:00
if (!animatedUiElement.RenderTransformOrigin.Equals(new Point(0.5, 0.5)))
{
animatedUiElement.SetCurrentValue(UIElement.RenderTransformOriginProperty, new Point(0.5, 0.5));
}
animatedUiElement.RenderTransform.BeginAnimation(TranslateTransform.YProperty, translateDoubleAnimation);
var opacityDoubleAnimation = new DoubleAnimation
{
Duration = duration,
DecelerationRatio = DecelerationRatio,
From = 0.0,
To = 1.0,
};
animatedUiElement.BeginAnimation(UIElement.OpacityProperty, opacityDoubleAnimation);
}
private static void SlideBottomTransition(UIElement animatedUiElement, Duration duration)
{
var translateDoubleAnimation = new DoubleAnimation
{
Duration = duration,
DecelerationRatio = DecelerationRatio,
From = 30,
To = 0,
};
2025-05-05 17:04:06 +08:00
if (animatedUiElement.RenderTransform is not TranslateTransform)
{
animatedUiElement.SetCurrentValue(UIElement.RenderTransformProperty, new TranslateTransform(0, 0));
}
2025-05-05 17:04:06 +08:00
if (!animatedUiElement.RenderTransformOrigin.Equals(new Point(0.5, 0.5)))
{
animatedUiElement.SetCurrentValue(UIElement.RenderTransformOriginProperty, new Point(0.5, 0.5));
}
animatedUiElement.RenderTransform.BeginAnimation(TranslateTransform.YProperty, translateDoubleAnimation);
}
private static void SlideLeftTransition(UIElement animatedUiElement, Duration duration)
{
var translateDoubleAnimation = new DoubleAnimation
{
Duration = duration,
DecelerationRatio = DecelerationRatio,
From = -50,
To = 0,
};
2025-05-05 17:04:06 +08:00
if (animatedUiElement.RenderTransform is not TranslateTransform)
{
animatedUiElement.SetCurrentValue(UIElement.RenderTransformProperty, new TranslateTransform(0, 0));
}
2025-05-05 17:04:06 +08:00
if (!animatedUiElement.RenderTransformOrigin.Equals(new Point(0.5, 0.5)))
{
animatedUiElement.SetCurrentValue(UIElement.RenderTransformOriginProperty, new Point(0.5, 0.5));
}
animatedUiElement.RenderTransform.BeginAnimation(TranslateTransform.XProperty, translateDoubleAnimation);
}
private static void SlideRightTransition(UIElement animatedUiElement, Duration duration)
{
var translateDoubleAnimation = new DoubleAnimation
{
Duration = duration,
DecelerationRatio = DecelerationRatio,
From = 50,
To = 0,
};
2025-05-05 17:04:06 +08:00
if (animatedUiElement.RenderTransform is not TranslateTransform)
{
animatedUiElement.SetCurrentValue(UIElement.RenderTransformProperty, new TranslateTransform(0, 0));
}
2025-05-05 17:04:06 +08:00
if (!animatedUiElement.RenderTransformOrigin.Equals(new Point(0.5, 0.5)))
{
animatedUiElement.SetCurrentValue(UIElement.RenderTransformOriginProperty, new Point(0.5, 0.5));
}
animatedUiElement.RenderTransform.BeginAnimation(TranslateTransform.XProperty, translateDoubleAnimation);
}
/// <summary>
2025-05-05 17:04:06 +08:00
/// 在向帧添加内容时尝试应用动画效果。
/// </summary>
/// <param name="element">Currently rendered element.</param>
/// <param name="type">Selected transition type.</param>
/// <param name="duration">Transition duration.</param>
/// <returns>Returns <see langword="true"/> if the transition was applied. Otherwise <see langword="false"/>.</returns>
public static bool ApplyTransition(object? element, Transition type, int duration)
{
2025-05-05 17:04:06 +08:00
if (type == Transition.None ||
!(RenderCapability.Tier >> 16 >= 0x1) ||
element is not UIElement uiElement ||
duration < 10)
{
return false;
}
duration = duration > 10000 ? 10000 : duration;
var timespanDuration = new Duration(TimeSpan.FromMilliseconds(duration));
2025-05-05 17:04:06 +08:00
switch (type)
{
case Transition.FadeIn:
FadeInTransition(uiElement, timespanDuration);
break;
case Transition.FadeInWithSlide:
FadeInWithSlideTransition(uiElement, timespanDuration);
break;
case Transition.SlideBottom:
SlideBottomTransition(uiElement, timespanDuration);
break;
case Transition.SlideRight:
SlideRightTransition(uiElement, timespanDuration);
break;
case Transition.SlideLeft:
SlideLeftTransition(uiElement, timespanDuration);
break;
default:
return false;
}
return true;
}
}