Animate TextView to increase integer and stop at some point?

前端 未结 5 1781
爱一瞬间的悲伤
爱一瞬间的悲伤 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);
            }
        }
    
    0 讨论(0)
  • 2020-12-12 22:10

    try this code..showing increment value with animation

    public class MainActivity extends Activity implements AnimationListener {
        private TextView textView;
        AlphaAnimation fadeIn, fadeOut;
    
        private static int count = 0, finalValue = 20;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.demo);
    
            textView = (TextView) findViewById(R.id.textView);
    
            fadeIn = new AlphaAnimation(0.0f, 1.0f);
            fadeOut = new AlphaAnimation(1.0f, 0.0f);
    
            fadeIn.setDuration(1000);
            fadeIn.setFillAfter(true);
            fadeOut.setDuration(1000);
            fadeOut.setFillAfter(true);
    
            fadeIn.setAnimationListener(this);
            fadeOut.setAnimationListener(this);
            textView.startAnimation(fadeIn);
            textView.startAnimation(fadeOut);
    
        }   
    
        @Override
        public void onAnimationEnd(Animation arg0) {
            // TODO Auto-generated method stub
    
            Log.i("mini", "Count:" + count);
    
            runOnUiThread(new Runnable() {
    
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    textView.setText("" + count);
                }
            });
    
            if (count == finalValue) {
                textView.setText("" + finalValue);
            } else {
                ++count;
                textView.startAnimation(fadeIn);
                textView.startAnimation(fadeOut);
            }
    
        }
    
        @Override
        public void onAnimationRepeat(Animation arg0) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onAnimationStart(Animation arg0) {
            // TODO Auto-generated method stub
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-12 22:15

    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.

    0 讨论(0)
  • 2020-12-12 22:21

    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();
    
    }
    
    0 讨论(0)
  • 2020-12-12 22:31

    This is a Kotlin code for incrementing from initial value to final value over a duration of time. Here I have used duration of 5 sec.

     fun animateTextView(initialValue: Int, finalValue: Int, textView: TextView) {
            val valueAnimator = ValueAnimator.ofInt(initialValue, finalValue)
            valueAnimator.duration = 5000 // 5 sec
            valueAnimator.addUpdateListener { valueAnimator -> textView.text = valueAnimator.animatedValue.toString() }
            valueAnimator.start()
        }
    

    use this code in Utilities and call the method accordingly with required parameters.

    0 讨论(0)
提交回复
热议问题