0

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.

mike
  • 1,318
  • 3
  • 21
  • 41

2 Answers2

0

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Doesn't that leave me with the problem of each element fading out, then becoming visible until the last one has faded out, then they all become invisible simultaneously? – mike Jun 29 '14 at 16:41
0

You need to implement a AnimationListener and then call the setVisibilty(View.INVISIBLE) after the animation is finished. You have to do this, because setting it to invisible is working right after you done the code. So it hides while it´s animating.

public class YourClass extends Foo implements AnimationListener {

//...

@Override
public void onAnimationEnd(Animation a) {
    // Do stuff.
    // SET INVISBLE HERE
}

@Override
public void onAnimationRepeat(Animation a) {    
}

@Override
public void onAnimationStart(Animation a) {
}

}

As in the chat mentioned, you can also add the AnimationListener on your Animation like:

fadeOut.setAnimationListener(new Animation.AnimationListener() { 
@Override 
public void onAnimationStart(Animation animation) { 

} 

@Override 
public void onAnimationEnd(Animation animation) { 

} 

@Override 
public void onAnimationRepeat(Animation animation) { 

} 
});
mapodev
  • 988
  • 8
  • 14
  • Where would this `AnimationListener` go? Within a class extending the `LayoutAnimationController`? – mike Jun 29 '14 at 16:47
  • Normally in the class where your gridView is. So let´s say your gridview is in the `onCreate(..)`. Then it should implement this class of the onCreate – mapodev Jun 29 '14 at 16:49
  • I'm confused, I've tried attaching `gridView.setLayoutAnimationListener()...` though this doesn't seem quite like what you suggest, can you explain a little more? – mike Jun 29 '14 at 16:55
  • No no, just add the `impements AnimationListener` in the Activity where you are working right now. And then add the unimplemented methods as listed above. – mapodev Jun 29 '14 at 16:57
  • Doesn't this listen for any animation at all within that Activity? There are other animations going on besides this one so this would be fired off more often than I would need it to – mike Jun 29 '14 at 17:07
  • yes that´s true, there for you have the parameter `Animation a` which you can check whether it is your animation. So yes, this method will be called whenever you run your animation, but this should be not a problem. – mapodev Jun 29 '14 at 17:09
  • I have made some edits but I still fail to see how this solves the problem. I need to set the individual views to invisible as soon as the animation is done, however, I do not have access to the individual views. I used the `LAC` because it made it easy to animate the GridView as a whole without having to look at individual views. Can you please provide some additional code? – mike Jun 29 '14 at 17:13
  • is the `onAnimationEnd` function fired? – mapodev Jun 29 '14 at 17:15
  • Hmmm now that I've double checked, it is not – mike Jun 29 '14 at 17:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56484/discussion-between-mike-and-mapo). – mike Jun 29 '14 at 17:17