Supreme Droids
Supreme Droids

Reputation: 31

number counting animation in textview

I am developing a game application in android. I am struck at Scorecard implementation. I want to display score in a textview as the numbers are continously counting for movement. for example : suppose user crosses one obstacle and score for that obstacle is 200. Then numbers in scorecard should count from 0 to 200 smoothly and then will stop at 200. I have seen this type of animation in score card of papiJump.

Please guide me.

Upvotes: 2

Views: 2288

Answers (2)

Muhammad Ali
Muhammad Ali

Reputation: 41

ValueAnimator animator   = ValueAnimator.ofInt(int start, int end);
              animator.setDuration(2000);
              animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    public void onAnimationUpdate(ValueAnimator animation) {
                        textView.setText(animation.getAnimatedValue().toString());
                    }
                });

                animator.start();

Upvotes: 4

Devloper Tamil
Devloper Tamil

Reputation: 1

public void animateTextView(int initialValue, int finalValue, final TextView textview)
 {
  DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator(0.8f);
        int start = Math.min(initialValue, finalValue);
        int end = Math.max(initialValue, finalValue);
        int difference = Math.abs(finalValue - initialValue);
        Handler handler = new Handler();
        for (int count = start; count <= end; count++) {
            int time = Math.round(decelerateInterpolator.getInterpolation((((float) count) / difference)) * 100) * count;
            final int finalCount = ((initialValue > finalValue) ? initialValue - count : count);
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    textview.setText(finalCount + "");
                }
            }, time);
        }
    }

Upvotes: 0

Related Questions