AnimationHelper.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. using System.Windows;
  5. using System.Windows.Media;
  6. using System.Windows.Media.Animation;
  7. using HandyControl.Tools.Extension;
  8. namespace HandyControl.Tools;
  9. /// <summary>
  10. /// 包含一些常用的动画辅助方法
  11. /// </summary>
  12. public class AnimationHelper
  13. {
  14. /// <summary>
  15. /// 创建一个Thickness动画
  16. /// </summary>
  17. /// <param name="thickness"></param>
  18. /// <param name="milliseconds"></param>
  19. /// <returns></returns>
  20. public static ThicknessAnimation CreateAnimation(Thickness thickness = default, double milliseconds = 200)
  21. {
  22. return new(thickness, new Duration(TimeSpan.FromMilliseconds(milliseconds)))
  23. {
  24. EasingFunction = new PowerEase { EasingMode = EasingMode.EaseInOut }
  25. };
  26. }
  27. /// <summary>
  28. /// 创建一个Double动画
  29. /// </summary>
  30. /// <param name="toValue"></param>
  31. /// <param name="milliseconds"></param>
  32. /// <returns></returns>
  33. public static DoubleAnimation CreateAnimation(double toValue, double milliseconds = 200)
  34. {
  35. return new(toValue, new Duration(TimeSpan.FromMilliseconds(milliseconds)))
  36. {
  37. EasingFunction = new PowerEase { EasingMode = EasingMode.EaseInOut }
  38. };
  39. }
  40. internal static void DecomposeGeometryStr(string geometryStr, out double[] arr)
  41. {
  42. var collection = Regex.Matches(geometryStr, RegexPatterns.DigitsPattern);
  43. arr = new double[collection.Count];
  44. for (var i = 0; i < collection.Count; i++)
  45. {
  46. arr[i] = collection[i].Value.Value<double>();
  47. }
  48. }
  49. internal static Geometry ComposeGeometry(string[] strings, double[] arr)
  50. {
  51. var builder = new StringBuilder(strings[0]);
  52. for (var i = 0; i < arr.Length; i++)
  53. {
  54. var s = strings[i + 1];
  55. var n = arr[i];
  56. if (!double.IsNaN(n))
  57. {
  58. builder.Append(n).Append(s);
  59. }
  60. }
  61. return Geometry.Parse(builder.ToString());
  62. }
  63. internal static Geometry InterpolateGeometry(double[] from, double[] to, double progress, string[] strings)
  64. {
  65. var accumulated = new double[to.Length];
  66. for (var i = 0; i < to.Length; i++)
  67. {
  68. var fromValue = from[i];
  69. accumulated[i] = fromValue + (to[i] - fromValue) * progress;
  70. }
  71. return ComposeGeometry(strings, accumulated);
  72. }
  73. internal static double[] InterpolateGeometryValue(double[] from, double[] to, double progress)
  74. {
  75. var accumulated = new double[to.Length];
  76. for (var i = 0; i < to.Length; i++)
  77. {
  78. var fromValue = from[i];
  79. accumulated[i] = fromValue + (to[i] - fromValue) * progress;
  80. }
  81. return accumulated;
  82. }
  83. }