简体   繁体   中英

ActionBar up button transition effect

I have already created a method for an animation when changing activities when the back button is pressed. The problem is that the actionbar up button has the default transition effect to the previous activity and I can't find a way to override that animation and use a new one. Any ideas? Thanks in advance

Preferably this would be hardcoded in java

Just get the event "home back"

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == android.R.id.home) {
        finish();
        overridePendingTransition(R.animator.anim_left, R.animator.anim_right);
        return true;
    }
    return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onBackPressed() {
    this.finish();
    overridePendingTransition(R.anim.fade_in, R.anim.right_slide_out);
}

fade_in.xml ( R.anim.fade_in )

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toAlpha="1.0" />

right_slide_out.xml ( R.anim.right_slide_out )

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator">
    <translate
        android:duration="200"
        android:fromXDelta="0"
        android:toXDelta="100%p" />
</set>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM