Android Simple TextView Animation

Rob picture Rob · Jan 31, 2012 · Viewed 8k times · Source

I've got a TextView that I would like to count down (3...2...1...stuff happens).

To make it a little more interesting, I want each digit to start at full opacity, and fade out to transparency.

Is there a simple way of doing this?

Answer

dmon picture dmon · Jan 31, 2012

Try something like this:

 private void countDown(final TextView tv, final int count) {
   if (count == 0) { 
     tv.setText(""); //Note: the TextView will be visible again here.
     return;
   } 
   tv.setText(String.valueOf(count));
   AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
   animation.setDuration(1000);
   animation.setAnimationListener(new AnimationListener() {
     public void onAnimationEnd(Animation anim) {
       countDown(tv, count - 1);
     }
     ... //implement the other two methods
   });
   tv.startAnimation(animation);
 }

I just typed it out, so it might not compile as is.