UIGradientAnimation.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using Microsoft.Windows.Forms.Animate;
  3. namespace Microsoft.Windows
  4. {
  5. /// <summary>
  6. /// 两段渐进动画动画
  7. /// </summary>
  8. public sealed class UIGradientAnimation : Animation<float>
  9. {
  10. private const float ANIMATION_DURATION = 500; //动画持续时间(毫秒)
  11. private const float FIRST_TIME = 0.5f * ANIMATION_DURATION; //第一段时间(毫秒)
  12. private const float FIRST_PERCENT = 0.875f; //第一段百分比
  13. private const float SECOND_TIME = ANIMATION_DURATION - FIRST_TIME; //第二段时间(毫秒)
  14. private const float SECOND_PERCENT = 1f - FIRST_PERCENT; //第二段百分比
  15. private float m_From;
  16. /// <summary>
  17. /// 获取或设置起点
  18. /// </summary>
  19. public float From
  20. {
  21. get { return this.m_From; }
  22. set { this.m_From = value; }
  23. }
  24. private float m_To;
  25. /// <summary>
  26. /// 获取或设置终点
  27. /// </summary>
  28. public float To
  29. {
  30. get { return this.m_To; }
  31. set { this.m_To = value; }
  32. }
  33. /// <summary>
  34. /// 构造函数
  35. /// </summary>
  36. public UIGradientAnimation()
  37. {
  38. }
  39. /// <summary>
  40. /// 获取当前滑动距离
  41. /// </summary>
  42. public override float Current
  43. {
  44. get
  45. {
  46. float distance = this.m_To - this.m_From;
  47. float escaped = (float)(DateTime.Now - this.StartTime).TotalMilliseconds;
  48. if (escaped < FIRST_TIME)//公式 第一段时间/第一段距离=流逝时间/移动距离
  49. return this.m_From + escaped / FIRST_TIME * distance * FIRST_PERCENT;
  50. if (escaped < ANIMATION_DURATION)//公式 第二段时间/第二段距离=(流逝时间-第一段时间)/(移动距离-第一段距离)
  51. return this.m_From + (escaped - FIRST_TIME) / SECOND_TIME * distance * SECOND_PERCENT + distance * FIRST_PERCENT;
  52. this.Stop();
  53. return this.m_To;
  54. }
  55. }
  56. /// <summary>
  57. /// 重新开始动画
  58. /// </summary>
  59. public void Next()
  60. {
  61. base.Start();
  62. }
  63. /// <summary>
  64. /// 释放资源
  65. /// </summary>
  66. /// <param name="disposing">释放托管资源为true,否则为false</param>
  67. protected override void Dispose(bool disposing)
  68. {
  69. }
  70. }
  71. }