How can I start layoutAnimation when(before) leaving an activity

醉酒当歌 提交于 2020-01-05 07:07:20

问题


I have a LinearLayout View in my activity.

When I press back button I want LinearLayout's children to slide out.

I have the following code which doesn't do anything:

private void SlideOut()
{
    LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(this, R.anim.layout_animation_row_slide_out);
    Animation animation=controller.getAnimation();
    animation.setFillAfter(true);
    LinearLayout menuLayout =((LinearLayout)findViewById(R.id.menuLayout));
    menuLayout.setLayoutAnimation(controller);
    menuLayout.startLayoutAnimation();
}

@Override
public void onBackPressed(){
    //super.onBackPressed();
    SlideOut();     
}

I have commented out super.OnBackPressed() to see if the animation starts, and it don't start.

Somebody with similar problems ?


回答1:


check if SlideOut() is called in onBackPressed - i'm guessing your back press is being handled elsewhere - possibly by the virtual keyboard




回答2:


try this.it may help you:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub

    if(keyCode==KeyEvent.KEYCODE_BACK){
    SlideOut();
    return true;
    }else{
        return false;
    }

    //return super.onKeyDown(keyCode, event);

}

can u just change the code here here :

private void SlideOut()
{
    Animation controller = AnimationUtils.loadLayoutAnimation(this, R.anim.layout_animation_row_slide_out);
   // Animation animation=controller.getAnimation();
    animation.setFillAfter(true);
    LinearLayout menuLayout =((LinearLayout)findViewById(R.id.menuLayout));
    menuLayout.startAnimation(controller);

}



回答3:


I think you might be exiting the activity before animation is finished. Try implementing Animation Listener



来源:https://stackoverflow.com/questions/8027572/how-can-i-start-layoutanimation-whenbefore-leaving-an-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!