I want to have a 2 second animation of an ImageView that spends 1000ms fading in and then 1000ms fading out.
Here's what I have so far in my ImageView constructor:
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setDuration(1000);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);
AnimationSet animation = new AnimationSet(true);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
this.setAnimation(animation);
When I run that animation, nothing shows up. However, when I remove one of the alpha animations, the behavior works as expected.
Things I have already tried:
- Every conceivable combination of
setFillBefore
,setFillAfter
, andsetFillEnabled
. - Adding a
LinearInterpolator
to theAnimationSet
.
As I believe in the power of XML (for layouts), this is the equivalent for the accepted answer, but purely as an animation resource:
The
fillAfter
is for the fade to remain after completing the animation. Theinterpolator
handles interpolation of the animations, as you can guess. You can also use other types of interpolators, like Linear or Overshoot.Be sure to start your animation on your view:
AnimationSets don't appear to work as expected at all. In the end I gave up and used the Handler class's postDelayed() to sequence animations.
Here is what I used to fade in/out Views, hope this helps someone.
You can also use animationListener, something like this: