Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement increasing number animation from 0 to 600 in 5 secs on TextVIew on android

I plan to implement integer number increase on textView from 0 to some value with animation within certain seconds. e.g show animation which increase number from 0 to 600 on textview for 5 seconds duration.

How can I implement this?

like image 774
Leon Li Avatar asked Apr 04 '15 07:04

Leon Li


People also ask

How to set animation on TextView in android?

To start the animation we need to call the startAnimation() function on the UI element as shown in the snippet below: sampleTextView. startAnimation(animation); Here we perform the animation on a textview component by passing the type of Animation as the parameter.

What is android animation?

Animations can add visual cues that notify users about what's going on in your app. They are especially useful when the UI changes state, such as when new content loads or new actions become available. Animations also add a polished look to your app, which gives it a higher quality look and feel.


3 Answers

You could use the ValueAnimator for that:

private void startCountAnimation() {
    ValueAnimator animator = ValueAnimator.ofInt(0, 600);
    animator.setDuration(5000);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        public void onAnimationUpdate(ValueAnimator animation) {
            textView.setText(animation.getAnimatedValue().toString());
        }
    });
    animator.start();
}
like image 148
Frederik Schweiger Avatar answered Oct 23 '22 04:10

Frederik Schweiger


Take a look at this simple solution:

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 22
Rafa0809 Avatar answered Oct 23 '22 03:10

Rafa0809


Use a ValueAnimator

TextView textview = findViewById(R.id.textview1);

ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 600);
valueAnimator.setDuration(5000);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
   @Override
   public void onAnimationUpdate(ValueAnimator valueAnimator) {
       textview.setText(valueAnimator.getAnimatedValue().toString());
   }
});
valueAnimator.start();
like image 5
Yasiru Nayanajith Avatar answered Oct 23 '22 02:10

Yasiru Nayanajith