kotlin 退出Activity 平滑动画

54 阅读1分钟

1. 先看方法

fun finishActivity() {
    finish()
    if (Build.VERSION.SDK_INT >= 34) {//34及以上的实现方式
        overrideActivityTransition(
            Activity.OVERRIDE_TRANSITION_OPEN,
            R.anim.xxx_from_left,
            R.anim.xxx_to_right
        )
    } else {//34以下的实现方式
        overridePendingTransition(R.anim.xxx_from_left, R.anim.xxx_to_right)
    }
}

2. 在资源的动画文件夹即anim下创建两个文件,即 xxx_from_left 和 xxx_to_right

xxx_from_left:

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="-100%p"
        android:toXDelta="0"
        android:duration="240" />
</set>

xxx_to_right:

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:toXDelta="100%p"
        android:duration="240" />
</set>