Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate TextView to increase integer and stop at some point?

Tags:

android

I have a TextView showing integer value. Integer value is transferred from previous activity, and I want to add nice animation. I want to if for example int value is 73, I want textView to increase shown number by 1 until 73, so it would be 1-2-3-4-5...etc etc. How can I do this?

like image 382
user3094736 Avatar asked Oct 22 '14 08:10

user3094736


3 Answers

The best solution in my opinion is to use this method :

public void animateTextView(int initialValue, int finalValue, final TextView  textview) {

    ValueAnimator valueAnimator = ValueAnimator.ofInt(initialValue, finalValue);
    valueAnimator.setDuration(1500);

    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {

            textview.setText(valueAnimator.getAnimatedValue().toString());

        }
    });
    valueAnimator.start();

}
like image 135
AhmedZah Avatar answered Nov 24 '22 00:11

AhmedZah


Here is a simple function to animate the text of a textView according to an initial and final value

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(String.valueOf(finalCount));
                }
            }, time);
        }
    }
like image 42
Pedro Oliveira Avatar answered Nov 23 '22 23:11

Pedro Oliveira


I think this project in github is what you want: https://github.com/sd6352051/RiseNumber

The RiseNumberTextView extends TextView and use the ValueAnimator to implement the rising number effect.

like image 43
Yong Avatar answered Nov 24 '22 00:11

Yong