自定义导航动画

Upv*_*ote 8 android android-animation android-actionbar

单击后退按钮时:

@Override
public void onBackPressed() {
    finish(); //go back to the previous Activity
    overridePendingTransition(R.anim.slide_in_exit, R.anim.slide_out_exit); 
}
Run Code Online (Sandbox Code Playgroud)

这将使视图生动.但是,当单击操作栏中的向上导航按钮时如何执行此操作?

Leo*_*y91 8

当我想在向上导航按钮上添加一些动画时,cYrixmorten的答案效果不佳,所以我重写了这个onOptionsItemSelected方法:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();
    switch(itemId){
        case android.R.id.home:
            super.onOptionsItemSelected(item);
            this.finish();
            overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
            break;
        default:
            break;
    }

    return true;
}
Run Code Online (Sandbox Code Playgroud)


cYr*_*ten 3

将 overridePendingTransition 放在 onCreate 中,以便在您离开 Activity 时发生转换。