Animation.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. namespace Microsoft.Windows.Forms.Animate
  3. {
  4. /// <summary>
  5. /// 动画
  6. /// </summary>
  7. /// <typeparam name="T">动画类型</typeparam>
  8. public abstract class Animation<T> : DisposableMini, IAnimation<T>
  9. {
  10. /// <summary>
  11. /// 标识动画停止的百分比
  12. /// </summary>
  13. protected const double STOPPED = 1d;
  14. private bool m_Stopped = true;
  15. /// <summary>
  16. /// 获取动画是否已停止
  17. /// </summary>
  18. public virtual bool Stopped
  19. {
  20. get
  21. {
  22. return this.m_Stopped;
  23. }
  24. }
  25. private DateTime m_StartTime;
  26. /// <summary>
  27. /// 获取动画开始时间
  28. /// </summary>
  29. public virtual DateTime StartTime
  30. {
  31. get
  32. {
  33. return this.m_StartTime;
  34. }
  35. }
  36. private TimeSpan m_Span;
  37. /// <summary>
  38. /// 获取或设置动画执行时间段,毫秒
  39. /// </summary>
  40. public virtual int Span
  41. {
  42. get
  43. {
  44. return (int)this.m_Span.TotalMilliseconds;
  45. }
  46. set
  47. {
  48. this.m_Span = new TimeSpan(0, 0, 0, 0, value);
  49. }
  50. }
  51. /// <summary>
  52. /// 获取当前帧
  53. /// </summary>
  54. public abstract T Current
  55. {
  56. get;
  57. }
  58. /// <summary>
  59. /// 获取动画完成百分比,如果等于 1 标志已结束
  60. /// </summary>
  61. protected virtual double Percentage
  62. {
  63. get
  64. {
  65. if (this.m_Stopped)
  66. return STOPPED;
  67. DateTime now = DateTime.Now;
  68. if (now >= this.m_StartTime + this.m_Span)
  69. {
  70. this.m_Stopped = true;
  71. return STOPPED;
  72. }
  73. return (now - this.m_StartTime).TotalMilliseconds / this.m_Span.TotalMilliseconds;
  74. }
  75. }
  76. /// <summary>
  77. /// 开始动画
  78. /// </summary>
  79. protected virtual void Start()
  80. {
  81. this.m_Stopped = false;
  82. this.m_StartTime = DateTime.Now;
  83. }
  84. /// <summary>
  85. /// 停止动画
  86. /// </summary>
  87. protected virtual void Stop()
  88. {
  89. this.m_Stopped = true;
  90. }
  91. }
  92. }