activity activity - 1 year ago 66
Android Question

fade out AlphaAnimation bug on Android > 5.0

The following code is used to fade out a View. It works for Android SDK < 21.

AlphaAnimation animOut = new AlphaAnimation(1, 0); //fromAlpha, toAlpha
animOut.setAnimationListener(new Animation.AnimationListener()
public void onAnimationStart(Animation animation){}
public void onAnimationRepeat(Animation animation){}
public void onAnimationEnd(Animation animation)

Strangely, it doesn't work as expected for Android 5.0 and above. As soon as animation starts, the animated View becomes invisible immediately.
How can I fix this?

Edit: this bug occurs if AlphaAnimation is started
of a TranslateAnimation.

Answer Source

You could use ViewPropertyAnimator.

Should work on newer APIs (12+) and you don't have to bother with setting the visibility of your View.

Something like this (to fade out):


To fade back in, just animate to your initial alpha value:


To add an AnimatorListener, you can call the setListener() method:

            .setListener(new Animator.AnimatorListener() {

    // ...