Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TextViewCounter extends ViewAnimator {
- private int countTo = 0; // The number to count to
- private int counter = 0; // The current counter
- public TextViewCounter(Context context) {
- super(context);
- init();
- }
- public TextViewCounter(Context context, AttributeSet attrs) {
- super(context, attrs);
- init();
- }
- private void init() {
- if (getInAnimation() != null) {
- getInAnimation().setAnimationListener(new Animation.AnimationListener() {
- @Override
- public void onAnimationStart(Animation animation) {
- // No op
- }
- @Override
- public void onAnimationEnd(Animation animation) {
- if (counter <= countTo) {
- showNext();
- }
- }
- @Override
- public void onAnimationRepeat(Animation animation) {
- // No op
- }
- });
- }
- }
- public TextCounter countTo(int countTo) {
- this.countTo = countTo;
- return this;
- }
- public void start() {
- showNext();
- }
- @Override
- public void showNext() {
- int nextIndex = getDisplayedChild() + 1;
- if (nextIndex >= getChildCount()) {
- nextIndex = 0;
- }
- ((TextView) getChildAt(nextIndex)).setText(Integer.toString(counter++));
- super.showNext();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement