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
.
If you use Animator for make animation you can
anim (directory) -> fade_out.xml
In java
Other way to make animation fade out with only java code is
Another alternative:
No need to define 2 animation for fadeIn and fadeOut. fadeOut is reverse of fadeIn.
So you can do this with Animation.REVERSE like this:
then onAnimationEnd:
Here is my solution using AnimatorSet which seems to be a bit more reliable than AnimationSet.
Figured out my own problem. The solution ended up being based in interpolators.
I know that this already has been answered but.....
Quick and easy way to quickly do a fade in and out with a self repeat. Enjoy
EDIT : In your activity add this: