我会在做一些研究后回答自己。你需要两件事:
1 - 通过在 AndroidManifest 中设置以下主题来创建“透明”活动。如果您不这样做,则会出现一个空白屏幕:
<style name="TransparentActivity" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
2. 手动创建一个 TranslationAnimation(或您想要的动画),将其应用到根 decorView,并通过将自定义动画设置为 0 来禁用它们:
overridePendingTransition(0, 0);
// Custom transition
TranslateAnimation translateAnimation = new TranslateAnimation(-translationLength, 0, 0, 0);
translateAnimation.setDuration(350);
translateAnimation.setFillAfter(true);
View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
rootView.startAnimation(translateAnimation);
rootView.getLayoutParams().width = translationLength;
如果您想要 onBackPressed 或活动完成时的反向动画:
@Override
public void finish() {
overridePendingTransition(0, 0);
TranslateAnimation translateAnimation = new TranslateAnimation(0, -translationLength, 0, 0);
translateAnimation.setDuration(350);
translateAnimation.setFillAfter(true);
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
YourActivity.super.finish();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
getWindow().getDecorView().findViewById(android.R.id.content).startAnimation(translateAnimation);
}