问题
I went through lots of tuts and examples for my requirement but still I think I am missing something somewhere. I am trying to hide and show a view with slide up and slide down animation.
Here is what I am looking for
fig 1 (before button 1 is clicked)
============================
| view1 |
============================
| button 1 |
============
fig 2 (after button 1 is clicked)
============================
| view1 |
============================
| |
| view 2 |
| |
============================
| button 1 |
============
As you can see I want to show and hide view2 when I click on button 1.
I have managed to do this by the following code. To hide:
TranslateAnimation slide = new TranslateAnimation(0, 0,0,-200);
slide.setDuration(1000);
slide.setFillAfter(true);
view2.startAnimation(slide);
view2.setVisibility(View.GONE);
To show:
TranslateAnimation slide = new TranslateAnimation(0, 0,-200,0);
slide.setDuration(1000);
slide.setFillAfter(true);
view2.startAnimation(slide);
view2.setVisibility(View.VISIBLE);
This works fine but the only problem in both the code is that the 'Gone' and 'Visible' code is completed before the animation is completed. i.e. 'button 1' moves down when clicked to show and the animation is completed later. same happens for hide, the 'button 1' moves up before the animation if completed.
I want to move the button along with the animation.
回答1:
the animation has the method setAnimationListener(AnimationListener). AnimationListener has tree callback
- onAnimationStart
- onAnimationRepeat
- onAnimationEnd
you can change the visibility when the onAnimationEnd is called.
For instance:
slide.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// here you can change the visibility
}
);
回答2:
Try out this code: ExpandAnimationExample from GitHub. I had the same problem, and this example solved my issue — maybe it will help you too.
来源:https://stackoverflow.com/questions/9602488/how-to-do-animations-in-views-in-android