Animate TextView to increase integer and stop at some point?

前端 未结 5 1798
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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
    
        }
    
    }
    

提交回复
热议问题