我有一个带DrawerLayout的活动,但每次打开它时都会有一点延迟,就像屏幕白了一下然后我的屏幕才被绘制。这发生在转换完成之后。所以看起来就像屏幕动画过渡出现了跳跃。
我尝试在使用ButterKnife绑定视图后的OnCreate中添加代码,但没有任何作用。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
postponeEnterTransition();
drawerLayout.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean onPreDraw() {
drawerLayout.getViewTreeObserver().removeOnPreDrawListener(this);
startPostponedEnterTransition();
return true;
}
});
}
是的,我正在为Lollipop进行优化,而对于早期的设备,我只是使用overridePendingTransitions
,它运行良好。我的问题仅出现在Lollipop设备上。
顺便说一句,我的进入和退出过渡都定义在xml中的fade_in_out
,并在styles
中指定。
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorAccent">@color/pink</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowActivityTransitions">true</item>
<item name="android:windowContentTransitions">true</item>
<!-- specify enter and exit transitions -->
<!-- options are: explode, slide, fade -->
<item name="android:windowEnterTransition">@transition/fade_in_out_transition</item>
<item name="android:windowExitTransition">@transition/fade_in_out_transition</item>
<!-- specify shared element transitions -->
<item name="android:windowSharedElementEnterTransition">@transition/change_clip_bounds</item>
<item name="android:windowSharedElementExitTransition">@transition/change_clip_bounds</item>
<item name="android:windowSoftInputMode">stateAlwaysHidden|adjustResize</item>
</style>