animate TextView to Number - Android User Interface

Android examples for User Interface:TextView Animation

Description

animate TextView to 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;/* w w w .j a  v a  2  s .  c  om*/
        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