Android Activity 过渡不适用于自定义动画

Android Activity Transition does not work with custom animations

我不明白为什么 overridePendingTransition 不能使用自定义动画。

在 MainActivity 中:

private void goToDetails() {
    Intent intent = new Intent(requireContext(), DetailsActivity.class);
    startActivity(intent);
}

详情活动

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);

以上代码按预期工作。但是,如果我从 android.R.anim.slide_in_left & slide_out_right 复制源文件,则将此行更改为:

overridePendingTransition(R.anim.my_slide_in_left, R.anim.my_slide_out_right);

然后动画不起作用 - 出现默认的向上滑动动画。我没有更改布局文件中的代码,我只是 copy/pasted 它们以便以后可以调整它们。

/res/anim/my_slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-50%p" android:toXDelta="0"
        android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

/res/anim/my_slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="50%p"
        android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

MainActivity 点击事件:

startActivity(new Intent(getContext(), NewReportActivity.class));
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

DetailActivity onBackPressed:

finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);