Animate TextView to increase integer and stop at some point?

前端 未结 5 1782
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-12 21:50

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 w

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 22:04

    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);
            }
        }
    

提交回复
热议问题