4

I have some views in the constraint layout. I used animation to reveal and hide the view. When the view is GONE, it blinks for a second and become visible and then gone.

     view.animate()
    .alpha(0.0f)
    .setDuration(300)
    .setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            view.setVisibility(View.GONE);
        }
    });
1

2 Answers 2

4

I tried your posted code and it seems to work just fine, except when animateLayoutChanges is turned on for the parent of the view you are trying to animate. If you have that enabled in the layout xml, turn it off and try again.

1
  • 3
    Just to elaborate on the answer here as I ran into this same problem. Using animateLayoutChanges in combination with the animation onAnimationEnd toggling the View visibility results in two animations running. The code runs once to set alpha to zero and the view disappears a first time. Then when the view is set to GONE in the onAnimationEnd code, a second animation runs from the layout's animateLayoutChanges setting that resets the alpha to 1 and fades out the view for a second time.
    – CodeSmith
    Commented Aug 19, 2020 at 18:03
0

This happens because of using AnimatorListenerAdapter, replace it with withEndAction:

        view.animate()
            .alpha(0.0f)
            .setDuration(300)
            .withEndAction(new Runnable() {
                @Override
                public void run() {
                    view.setVisibility(View.GONE);
                }
            });

Not the answer you're looking for? Browse other questions tagged or ask your own question.