如何消除使用 DrawerLayout 打开 Activity 时的延迟?

How to remove the delay when opening an Activity with a DrawerLayout?

我有一个带有 DrawerLayout 的 activity,但每当它打开时,都会有一个延迟,如屏幕为白色然后我的屏幕被绘制的瞬间。

这发生在过渡完成后。所以它看起来有点像屏幕动画转换在跳跃。

我尝试在使用 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 优化它,对于 Lollipop 之前的设备,我只是使用 overridePendingTransitions 并且它工作正常。我的问题只出现在 Lollipop 设备上。

顺便说一句,我的进入和退出转换都是 fade_in_out 在 xml 中定义并在 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>

可能是因为 lollipop 在 UI 个元素上有默认的 layoutTransition,你试过了吗?

drawerLayout.setLayoutTransition(null)

我会更改您的退出过渡:

item name="android:windowExitTransition">@transition/fade_in_out_transition</item>
item 

至 window return:

name="android:windowReturnTransition">@transition/fade_in_out_transition</item>
  1. 当您使用 window 退出时,window 的可见性会在您的下一次转换开始之前暂时变为不可见。

Sets the Transition that will be used to move Views out of the scene when the fragment is removed, hidden, or detached when not popping the back stack. The exiting Views will be those that are regular Views or ViewGroups that have isTransitionGroup() return true. Typical Transitions will extend Visibility as exiting is governed by changing visibility from VISIBLE to INVISIBLE. If transition is null, the views will remain unaffected.

setExitTransition

  1. 返回转换处理 window 关闭,而不是退出并且不影响 window 可见性。

Reference to a Transition XML resource defining the desired Transition used to move Views out of the scene when the Window is preparing to close. Corresponds to setReturnTransition(android.transition.Transition).

android:windowReturnTransition

  1. 我还建议使用 reenter 来管理后压。

Reference to a Transition XML resource defining the desired Transition used to move Views in to the scene when returning from a previously-started Activity. Corresponds to setReenterTransition(android.transition.Transition).

android:windowReenterTransition

Understanding exit/reenter shared element transitions

  1. 您还可以设置一个允许过渡重叠的布尔值,但重叠可能太长 为你所想。

setAllowEnterTransitionOverlap(boolean)

  1. 我也会将棒棒糖升级到 5.0.1
    5.0.0有bug,5.0.1已经修复

Linton Ye 的这篇博客详细介绍了有关 Lollipop 转换和错误的问题。

My Journey to Lollipop Transitions: part 1

我终于找到了解决办法。我不知道它为什么或如何成功,但我只知道它消除了动画中的延迟。我在 activity 的 OnCreate 中添加了一个处理程序,它将 运行 用于设置的其他语句,即将初始片段添加到视图中,在 300 毫秒后

Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        switchFragment();
    }
}, 300);