46

我对 WebView(Android 3.0+)有疑问,WebView 在显示我的黑色背景(“闪烁”)之前总是显示白色背景。这是我的简单测试代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    WebView webView = new WebView(this);
    webView.setBackgroundColor(Color.BLACK);
    setContentView(webView);
    loadWebView(webView);
    webView.loadDataWithBaseURL("localhost://", "<html><head>" +
            "<style>body {background-color: #000}img{max-width:100%}</style></head>" +
            "<body>" +
            "<img src=\"http://developer.android.com/images/practices/actionbar-phone-splitaction.png\" />" +
            "</body></html>", 
            "text/html", "UTF-8", null);
}

我已经尝试了很多解决方案来摆脱这个问题,但并不幸运。

PS:关闭硬件加速不会出现该问题。有没有人遇到同样的问题并解决了?

谢谢你。

4

7 回答 7

53

我发现最有效的解决方法是在布局膨胀后设置透明背景颜色,首先在这里提到:

webView.setBackgroundColor(Color.argb(1, 0, 0, 0));

是的,这是一个彻底的黑客攻击,但它是我发现在不禁用硬件加速的情况下运行良好的唯一解决方案。

请注意,这不适用于在 XML 中设置背景。

这已在 Jellybean 中得到解决,尽管一些用户报告在 KitKat 中看到了这一点。检查您是否没有禁用硬件加速,如果问题确实消失了,您可能希望将该代码包装在条件语句中以仅针对旧设备:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
    webView.setBackgroundColor(Color.argb(1, 0, 0, 0));
}
于 2013-04-25T14:30:43.713 回答
13

我为应用程序启用了硬件加速,并为活动禁用了它。null另外,如上所述,我将背景设置为 。它现在对我有用。

另一种方法(未经测试):将图层类型设置为软件渲染并将背景设置为Color.TRANSPARENT(或0):webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

格雷茨托尔斯滕

于 2012-06-19T14:25:11.907 回答
9

在 Android 4.4.4 上遇到了这个问题,这里没有其他解决方案有效。无论如何我都在搞乱onPageFinished(),所以不妨尝试一下应该是防弹的东西:

我把它放在onCreateView()承载 WebView 的片段中:

    webView = (WebView) v.findViewById(R.id.webView);
    webView.setVisibility(View.INVISIBLE);
    webView.setBackgroundColor(Color.argb(1, 0, 0, 0));         

    webView.setWebViewClient( new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            view.setVisibility(View.VISIBLE);
            super.onPageFinished(view, url);
        }
    });

这个想法只是在第一页加载之前隐藏 WebView。但是,它仍然给了我一点白色的闪光,直到我还添加了 Paul Lammertsma 提供的解决方案,webView.setBackgroundColor(Color.argb(1, 0, 0, 0));

于 2015-03-30T21:41:09.180 回答
5

这显然是Android版本> 3的可怕“功能”。甚至官方应用谷歌阅读器也包含这个白色闪光。解决方案是禁用 Activity 的硬件加速,您需要使用快速绘图 Web 视图。

于 2012-09-10T13:55:00.260 回答
1

就这样吧:

WebView web=(WebView)findViewById(R.id.web);
web.setBackgroundColor(0xff000000);
于 2018-06-06T06:38:16.050 回答
0

网页视图有类似的问题。我在 Ice Cream Sandwich 工作,并将 minSdk = 13 放入 Android Manifest 对我有用。尝试对此进行试验。我猜SDK中有一些错误。

于 2012-10-05T17:20:19.277 回答
0

现在是 2021 年,我仍然为此苦苦挣扎,并想分享我的经验。我发现的解决方案都没有奏效。我不能关闭 webview 的硬件加速,因为这意味着太多的性能损失。在尝试了一堆解决方案之后,我意识到不可能将 webview 的可见性从 View.INVISIBLE 更改为 View.VISIBLE 而不会导致闪烁/闪烁/闪烁。所以,我让 webview 可见。但是,我在布局文件中将 webview 的半透明设置为透明。在加载 url 时,webview 是透明的。加载页面后,我将 webview 设置为不透明以在屏幕上显示 webview。由于 webview 可见性没有变化,因此不存在闪烁/闪烁/闪烁问题。

布局文件:

    <WebView
        android:id="@+id/WebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:alpha="0.0"
        android:background="@android:color/transparent"
    />

MainActivity.kt:

    mWebView.webViewClient = object : WebViewClient() {
        override fun onPageFinished(view: WebView?, url: String?) {
            super.onPageFinished(view, url)

            // code for other stuff

            // Change the webview translucency from transparent to opaque.
            // This is a workaround of the Android bug of webview blinking when hardware acceleration is turned on
            // Basically, we cannot change the webview visibility to show and hide the webview.
            // Doing so would cause flickering/blinking with hardware acceleration).
            // Instead, the webview is visible the entire time.
            // We set the webview to be transparent while loading a page and then change the webview
            // to opaque once the page is loaded to make the webview visible to the user.
            if (mWebView.alpha < 1.0F) {
                mWebView.alpha = 1.0F
            }

            // more code for other stuff
        }
    }
于 2021-10-16T06:13:10.063 回答