CustomAnimationDrawable.java 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package com.khmer9.lotto.util;
  2. import android.graphics.drawable.AnimationDrawable;
  3. import android.os.Handler;
  4. public abstract class CustomAnimationDrawable extends AnimationDrawable {
  5. /** Handles the animation callback. */
  6. Handler mAnimationHandler;
  7. public CustomAnimationDrawable(AnimationDrawable aniDrawable) {
  8. /* Add each frame to our animation drawable */
  9. for (int i = 0; i < aniDrawable.getNumberOfFrames(); i++) {
  10. this.addFrame(aniDrawable.getFrame(i), aniDrawable.getDuration(i));
  11. }
  12. }
  13. @Override
  14. public void start() {
  15. super.start();
  16. /*
  17. * Call super.start() to call the base class start animation method.
  18. * Then add a handler to call onAnimationFinish() when the total
  19. * duration for the animation has passed
  20. */
  21. mAnimationHandler = new Handler();
  22. mAnimationHandler.post(new Runnable() {
  23. @Override
  24. public void run() {
  25. onAnimationStart();
  26. }
  27. });
  28. mAnimationHandler.postDelayed(new Runnable() {
  29. @Override
  30. public void run() {
  31. onAnimationFinish();
  32. }
  33. }, getTotalDuration());
  34. }
  35. /**
  36. * Gets the total duration of all frames.
  37. *
  38. * @return The total duration.
  39. */
  40. public int getTotalDuration() {
  41. int iDuration = 0;
  42. for (int i = 0; i < this.getNumberOfFrames(); i++) {
  43. iDuration += this.getDuration(i);
  44. }
  45. return iDuration;
  46. }
  47. /**
  48. * Called when the animation finishes.
  49. */
  50. public abstract void onAnimationFinish();
  51. /**
  52. * Called when the animation starts.
  53. */
  54. public abstract void onAnimationStart();
  55. }