简单的动画不起作用

时间:2014-05-03 09:08:30

标签: android android-animation

我有以下代码:

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); 不起作用。视图关闭时没有任何动画。 我希望动画首先出现,然后关闭视图。 我该如何实现它? 我想我必须使用AnimationListner。 有人可以帮我使用AnimationListner代码吗?

1 个答案:

答案 0 :(得分:0)

我认为问题可能是View.GONE不等到动画slideDown完成(所以你看不到它)...试着把它当作

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);