Make activity animate from top to bottom

前端 未结 4 1602
庸人自扰
庸人自扰 2020-12-04 13:00

I am writing an Android app where I want the activity to appear by animating in from the bottom of the screen to the top. I am able to do this with code from here:

4条回答
  •  长情又很酷
    2020-12-04 13:21

    You need to define your "slide up" animations from the linked question, and some new "slide down" animations that reverse the process.

    The important parts of the animations to look at are the fromYDelta and toYDelta values. These define the Y-positions (of the top of your view) at the start & end of the animations.

    slide_in_up.xml

    
    
    

    slide_out_up.xml

    
    
    

    slide_in_down.xml

    
    
    

    slide_out_down.xml

    
    
    

    For the "slide up" animations, you should have overridden the pending transition in your onResume() method like this:

    protected void onResume()
    {
        super.onResume();
        overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);
    }
    

    For the "slide down" animations, do something similar in your onPause() method:

    protected void onPause()
    {
        super.onPause();
        overridePendingTransition(R.anim.slide_in_down, R.anim.slide_out_down);
    }
    

    Some tutorials suggest using the wrong life-cycle methods:

    • onCreate() is not called every time the activity is shown
    • onDestroy() is not called every time the activity is taken away

    Rather use methods that are called every time there is a screen transition:

    • onResume() is called when the activity is shown to the user
    • onPause() is called when the activity is going to be taken away

    For more info on these methods specifically, check the Android developer site:

    • Pausing and Resuming an Activity

    When your screen is displayed, it will slide in from the bottom.

    When a new screen is displayed, your screen will slide back down.

提交回复
热议问题