Search code examples
androidgridviewlayout-animation

How to change visibility of individual Views as layoutAnimationController animates them?


I have a GridView and when a button is pressed I want all of the elements to fade out one at a time (or fade in, if they are already invisible). On the fade in end, I have:

Animation fadeIn = AnimationUtils.loadAnimation(activity, R.anim.fade_in);
LayoutAnimationController layoutAnimationController = new LayoutAnimationController(fadeIn);
gridView.setLayoutAnimation(layoutAnimationController);
gridView.setVisibility(View.VISIBLE);

Works as intended. Each view in the GridView fade in one at a time and then the entire GridView is visible. On the fade out end, I though something similar might work:

Animation fadeOut = AnimationUtils.loadAnimation(activity, R.anim.fade_out);
LayoutAnimationController layoutAnimationController = new LayoutAnimationController(fadeOut);
gridView.setLayoutAnimation(layoutAnimationController);
gridView.setVisibility(View.INVISIBLE);

However, this just makes the GridView instantly disappear, no animation takes place, it just becomes invisible. Why is this? I think I saw a method of extending the LayoutAnimationController wherein I can specify what the visibility is after animation but is there an easier way to get the fade out I want?

EDIT: So basically, as each view gets animated with a particular animation, I want it to change status to either VISIBLE or INVISIBLE, I suppose if the entire contents of the GridView are INVISIBLE I don't really care about the GridView itself being invisible. For some reason, this is the exact behavior I get on the fadeIn but on the fadeOut things don't work as nicely, I'm guessing the fadeIn working is just a silly coincidence and there is actually a nicer way of doing this.

I realize one way is to manually write all the animations for every View visible on the screen but if this can be achieved via the LayoutAnimationController, that's obviously much better.

EDIT: The jist of my problem: onButtonPressed, I want the Views present in a GridView to fade out one at a time and the entire GridView to be invisible at the end of the animation.


Solution

  • As soon as you set the visibility, it changes immediately (well, as soon as the UI thread returns to the Looper). If you want it to go invisible after the animation, you need to not set the visibility until the animation is done. Probably making the last frame of the animation set it is sufficient.