Advertisement
Guest User

Untitled

a guest
Dec 28th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. public class TextViewCounter extends ViewAnimator {
  2.  
  3. private int countTo = 0; // The number to count to
  4. private int counter = 0; // The current counter
  5.  
  6. public TextViewCounter(Context context) {
  7. super(context);
  8. init();
  9. }
  10.  
  11. public TextViewCounter(Context context, AttributeSet attrs) {
  12. super(context, attrs);
  13. init();
  14. }
  15.  
  16. private void init() {
  17. if (getInAnimation() != null) {
  18. getInAnimation().setAnimationListener(new Animation.AnimationListener() {
  19. @Override
  20. public void onAnimationStart(Animation animation) {
  21. // No op
  22. }
  23.  
  24. @Override
  25. public void onAnimationEnd(Animation animation) {
  26. if (counter <= countTo) {
  27. showNext();
  28. }
  29. }
  30.  
  31. @Override
  32. public void onAnimationRepeat(Animation animation) {
  33. // No op
  34. }
  35. });
  36. }
  37. }
  38.  
  39. public TextCounter countTo(int countTo) {
  40. this.countTo = countTo;
  41. return this;
  42. }
  43.  
  44. public void start() {
  45. showNext();
  46. }
  47.  
  48. @Override
  49. public void showNext() {
  50. int nextIndex = getDisplayedChild() + 1;
  51. if (nextIndex >= getChildCount()) {
  52. nextIndex = 0;
  53. }
  54.  
  55. ((TextView) getChildAt(nextIndex)).setText(Integer.toString(counter++));
  56. super.showNext();
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement