Advertisement
Guest User

Untitled

a guest
May 28th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. /**
  2. * Provides a callback when a non-looping {@link AnimationDrawable} completes its animation sequence. More precisely,
  3. * {@link #onAnimationCompleted()} is triggered when {@link View#invalidateDrawable(Drawable)} has been called on the
  4. * last frame.
  5. *
  6. * @author Benedict Lau
  7. */
  8. public abstract class AnimationDrawableCallback implements Callback {
  9.  
  10. /**
  11. * The total number of frames in the {@link AnimationDrawable}.
  12. */
  13. private final int mTotalFrames;
  14.  
  15. /**
  16. * The last frame of {@link Drawable} in the {@link AnimationDrawable}.
  17. */
  18. private final Drawable mLastFrame;
  19.  
  20. /**
  21. * The current frame of {@link Drawable} in the {@link AnimationDrawable}.
  22. */
  23. private int mCurrentFrame = 0;
  24.  
  25. /**
  26. * The client's {@link Callback} implementation. All calls are proxied to this wrapped {@link Callback}
  27. * implementation after intercepting the events we need.
  28. */
  29. private Callback mWrappedCallback;
  30.  
  31. /**
  32. * Flag to ensure that {@link #onAnimationCompleted()} is called only once, since
  33. * {@link #invalidateDrawable(Drawable)} may be called multiple times.
  34. */
  35. private boolean mIsCallbackTriggered = false;
  36.  
  37. /**
  38. * Constructor.
  39. *
  40. * @param animationDrawable the {@link AnimationDrawable}.
  41. * @param callback the client's {@link Callback} implementation. This is usually the {@link View} the has the
  42. * {@link AnimationDrawable} as background.
  43. */
  44. public AnimationDrawableCallback(AnimationDrawable animationDrawable, Callback callback) {
  45. mTotalFrames = animationDrawable.getNumberOfFrames();
  46. mLastFrame = animationDrawable.getFrame(mTotalFrames - 1);
  47. mWrappedCallback = callback;
  48. }
  49.  
  50. @Override
  51. public void invalidateDrawable(Drawable who) {
  52. if (mWrappedCallback != null) {
  53. mWrappedCallback.invalidateDrawable(who);
  54. }
  55.  
  56. if (!mIsCallbackTriggered && mLastFrame != null && mLastFrame.equals(who.getCurrent())) {
  57. mIsCallbackTriggered = true;
  58. onAnimationCompleted();
  59. }
  60. }
  61.  
  62. @Override
  63. public void scheduleDrawable(Drawable who, Runnable what, long when) {
  64. if (mWrappedCallback != null) {
  65. mWrappedCallback.scheduleDrawable(who, what, when);
  66. }
  67.  
  68. onAnimationAdvanced(mCurrentFrame, mTotalFrames);
  69. mCurrentFrame++;
  70. }
  71.  
  72. @Override
  73. public void unscheduleDrawable(Drawable who, Runnable what) {
  74. if (mWrappedCallback != null) {
  75. mWrappedCallback.unscheduleDrawable(who, what);
  76. }
  77. }
  78.  
  79. //
  80. // Public methods.
  81. //
  82.  
  83. /**
  84. * Callback triggered when a new frame of {@link Drawable} has been scheduled.
  85. *
  86. * @param currentFrame the current frame number.
  87. * @param totalFrames the total number of frames in the {@link AnimationDrawable}.
  88. */
  89. public abstract void onAnimationAdvanced(int currentFrame, int totalFrames);
  90.  
  91. /**
  92. * Callback triggered when {@link View#invalidateDrawable(Drawable)} has been called on the last frame, which marks
  93. * the end of a non-looping animation sequence.
  94. */
  95. public abstract void onAnimationCompleted();
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement