Android: Fade view in and out

DominicM picture DominicM · May 28, 2013 · Viewed 41.7k times · Source

I need to display an image button that fades in and out (and in and out and so on...) The transparency can be set with setAlpha but how can I fade in and out? I mean I cannot do it on another thread because you need to do such things on the UI thread, right?

I guess it can be done with animations but I haven't found anything, because I don't have any experience with animations and don't really know what to search for...

Actually what I really want is to fade one image in and another one out but I guess the easiest way is to place the first image button below the second and just fade the second one. Or is there an easier way to do it?

Answer

DominicM picture DominicM · Jun 24, 2013

Here is the solution I'm using now, that works on API level lower than 12:

AlphaAnimation anim = new AlphaAnimation(1.0f, 0.0f);
anim.setDuration(1000);
anim.setRepeatCount(NUM_REPEATS);
anim.setRepeatMode(Animation.REVERSE);
button.startAnimation(anim);