使用底部导航栏的意图时淡入淡出幻灯片动画

Fade slide animation when using intent for bottom navigation bar

我在底部有一个带图标的导航栏。当我点击一个图标时,一个新的 activity 以 intent 开始。

context.startActivity(intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP));

像这样。 如果我在我的 Android 像素模拟器上测试它,当新的 activity 开始时,动画总是从下到上。 如果我在我的 android 物理设备 (Android 7.1) 上测试它,当新的 activity 开始时,动画总是从右到左。当我关闭 activity 时,动画会从左到右。

我总是想要这个幻灯片动画(就像在我的物理设备上一样)。我需要做什么?

我加了

        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);

在活动的 onCreate() 中,所以我在 activity 开始时有幻灯片,但在 activity 关闭时没有幻灯片动画。

感谢您的帮助

您需要在 anim 文件夹中添加 2 个新的动画文件,并在 Activity 中添加一些您希望随动画一起关闭的代码,添加对 onBackPressed 方法的更改

首先是动画文件:left_to_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:duration="500"
        android:fromXDelta="-100%"
        android:fromYDelta="0%"
        android:toXDelta="0%"
        android:toYDelta="0%" />
</set>

right_to_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:duration="500"
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:toXDelta="100%"
        android:toYDelta="0%" />
</set>

并在 Activity 中执行以下操作:

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
}

PS 另请注意,即使您的代码没问题,您的 phone 也可能关闭了动画