I have the following code:

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);
    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener()
    {
        Animation slideUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
        Animation slideDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);
        public void onClick(View v) {
            Log.v("Hide","ButtonClicked");
            if(layout.getVisibility()==View.VISIBLE){
                layout.startAnimation(slideDown); //This is not Working
                layout.setVisibility(View.GONE); //This Works
                }
            else {
                layout.setVisibility(View.VISIBLE); //This Works
                layout.startAnimation(slideUp); //This Works
            }
        }});

}
}

layout.startAnimation(slideDown); is not working. The View closes without any animation. I want the animation to occur first and the then the view is closed. How can I implement it? I think I have to use AnimationListner. Can anybody help me with the AnimationListner Code?

有帮助吗?

解决方案

i think that the problem may be that View.GONE does not wait until the animation slideDown is finished (so you cant see it)... try to put it like

slideDown .setAnimationListener(new AnimationListener(){

   @Override
   public void onAnimationStart(Animation animation){}

   @Override
   public void onAnimationRepeat(Animation animation){}

   @Override
   public void onAnimationEnd(Animation animation){
      layout.setVisibility(View.GONE);
   }
});
layout.startAnimation(slideDown);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top