我想为下一个 Activity 添加一个进入过渡。
所以我做了:
getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
window.setEnterTransition(new Slide());
这似乎行不通。在做了一些试验和错误之后(因为我在其他 Activity 上进行了此转换)我发现它在调用后确实有效
ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, view, "some_name");
ActivityCompat.startActivity(activity, new Intent(TourAndLoginActivity.this, LoginActivity.class), activityOptionsCompat.toBundle());
但是我没有共享元素(我添加了一个 View 只是为了测试它)。 无法将“null”添加为共享元素。
真的必须这样做吗?我的解决方法是添加一个不可见的共享元素。
最佳答案
摘自 android 开发者文档:
Start an activity using transitions If you enable transitions and set an exit transition for an activity, the transition is activated when you launch another activity as follows:
startActivity(intent,
ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
If you have set an enter transition for the second activity, the transition is also activated when the activity starts. To disable transitions when you start another activity, provide a null options bundle.
https://developer.android.com/training/material/animations.html
所以首先像下面这样启用转换:
getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
window.setEnterTransition(new Slide());
然后开始 Activity 如下:
startActivity(intent,
ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
关于android - `setEnterTransition` 仅适用于 `ActivityCompat.startActivity`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37607973/