animate TextView Number - Android android.widget

Android examples for android.widget:TextView

Description

animate TextView Number

Demo Code

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;

public class Main{

    public static void animTextViewNum(final TextView tv, int toNum) {

        Integer fromNum;//  ww  w  . j  a  va2 s  .  c o m
        String fromNumStr = tv.getText().toString();
        if (TextUtils.isEmpty(fromNumStr)) {
            fromNum = 0;
        } else {
            fromNum = Integer.parseInt(fromNumStr);
        }

        ValueAnimator animator = ValueAnimator.ofInt(fromNum, toNum);
        animator.setTarget(tv);

        animator.setInterpolator(new AccelerateInterpolator());
        animator.setDuration(1000).start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                String num = animation.getAnimatedValue().toString();
                tv.setText(num);
            }
        });
    }

}

Related Tutorials