Make activity animate from top to bottom

前端 未结 4 1594
庸人自扰
庸人自扰 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:28

    Two ways of doing this:

    1. Using styles

    Assuming you wish to implement this for all activities, in your base theme define the following entry:

    @style/ActivityAnimations

    Then define the following style:

    
    

    Where @anim/hold can be something like this:

    
        
    
    

    2. Using overridePendingTransition()

    Override finish():

      @Override
      public void finish() {
        super.finish();
        overridePendingTransition(R.anim.hold, R.anim.disappear_to_bottom);
      }
    

    Override onBackPressed():

      @Override
      public void onBackPressed() {
        finish();
      }
    

    Override onOptionsItemSelected():

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
          case android.R.id.home:
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
      }
    

提交回复
热议问题