在设备上动画视图时,Android WebView 会出现闪烁错误。

5
我正在尝试实现类似Facebook、Path或Google+ Android应用程序的滑动导航。可以在此处找到完整的源代码以重现此错误。
然而,在设备上将WebView动画化移出屏幕时,会出现闪烁,就好像设备在将其推出屏幕之前咬了一口我的WebView一样。

WebView slide nav menu IN (device)

这个问题在模拟器上不会出现!动画可以平稳地进行。

WebView slide nav menu IN (emulator)

有趣的是,当动画的视图是 WebView 时,似乎只会出现闪烁!这里是使用 TextView 作为主要内容视图的相同项目。

TextView slide nav menu IN (device)

网页视图活动的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue" >

    <WebView
        android:id="@+id/web_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

对于文本视图活动:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue" >

    <TextView
        android:id="@+id/web_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello there. no flicker!"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
         />

</RelativeLayout>

调用以滑动导航菜单的代码:

 private void slideIn() {
        LinearLayout actionBarFrame = (LinearLayout) findViewById(android.R.id.content).getParent();

        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        menuView = inflater.inflate(R.layout.menu, null);
        menuView.setLayoutParams(new FrameLayout.LayoutParams(SIZE, LayoutParams.MATCH_PARENT, Gravity.LEFT));
        FrameLayout decorView = (FrameLayout) getWindow().getDecorView();
        decorView.addView(menuView);
        decorView.bringChildToFront(actionBarFrame);

        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) actionBarFrame.getLayoutParams();
        params.setMargins(SIZE, 0, -SIZE, 0);
        actionBarFrame.setLayoutParams(params);

        TranslateAnimation ta = new TranslateAnimation(-SIZE, 0, 0, 0);
        ta.setDuration(DURATION); 
        actionBarFrame.startAnimation(ta);
    }

并将其滑动回去:

    private void slideOut() {
        LinearLayout actionBarFrame = (LinearLayout) findViewById(android.R.id.content).getParent();

        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) actionBarFrame.getLayoutParams();
        params.setMargins(0, 0, 0, 0);
        actionBarFrame.setLayoutParams(params);

        TranslateAnimation ta = new TranslateAnimation(SIZE, 0, 0, 0);
        ta.setDuration(DURATION);
        ta.setAnimationListener(new  AnimationListener() {
            @Override 
            public void onAnimationEnd(Animation arg0) {
                ((FrameLayout) getWindow().getDecorView()).removeView(menuView);
                menuView = null;
            }
            @Override public void onAnimationRepeat(Animation arg0) { }
            @Override public void onAnimationStart(Animation arg0) { }
        });
        actionBarFrame.startAnimation(ta);
    }

我已将代码上传至 BitBucket 作为公共仓库,所以您可以像我一样尝试此项目。
非常感谢任何关于为什么会发生这种情况或如何修复它的帮助或想法!期望的是平稳地将 WebView 动画移出屏幕并再次移回。谢谢!

这里也有同样的问题。@Robert,你找到解决方案了吗? - jayellos
1
@jayellos 这个问题有两种潜在的解决方法。我会在下面附上一个答案,但故事的寓意是,在 WebView 上启用硬件加速可以帮助解决问题(某些平台上默认未启用),或使用此库(我们目前的解决方案)。 - Robert Karl
1个回答

8
一般来说,这个问题似乎与在启用硬件加速的3.0+设备上使用WebViews时出现的bug有关。我也尝试使用滑动菜单库与WebView一起使用,但是遇到了同样的闪烁问题。在寻找解决方法时,我在这里发现了一个解决方法: 如果启用了硬件加速(Android 3.0+),则WebView会闪烁显示白色背景 该解决方法使用以下代码将WebView的层类型设置为软件渲染:
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

这段代码解决了闪烁问题,但牺牲了性能。

1
我可以确认这个解决方法是有效的。有人找到了允许硬件加速的解决方案吗? - Gibberish

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接