WebView“闪烁"如果启用硬件加速,则为白色背景(Android 3.0+) [英] WebView "flashing" with white background if hardware acceleration is enabled (Android 3.0+)

本文介绍了WebView“闪烁"如果启用硬件加速,则为白色背景(Android 3.0+)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an issue with the WebView (Android 3.0+), which the WebView always displays a white background before display my black background ("flashing"). Here is my simple test code:

@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);
}

我尝试了很多解决方案来摆脱这个问题,但都不走运.

I have tried many solutions to get rid of this problem, but not lucky.

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

PS: The problem will not appear if the hardware acceleration is turned off. Have anybody have the same problem and solved it?

谢谢.

推荐答案

我找到了最有效的解决方法,这里第一次提到,是在布局膨胀后设置透明背景色:

I found the most effective fix for this, first mentioned here, was to set a transparent background color after the layout has been inflated:

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

是的,这完全是一种黑客行为,但它是我发现的唯一一种在不禁用硬件加速的情况下运行良好的解决方案.

Yes, it's a total hack, but it's the only solution I've found to work well without disabling hardware acceleration.

请注意,通过在 XML 中设置背景,这不能起作用.

Note that this does not work through setting the background in XML.

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

This has been resolved in Jellybean, although some users have reported seeing this in KitKat. Check that you have not disabled hardware acceleration, and if the problem indeed disappears, you will probably want to wrap that code in a conditional statement to only target older devices:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
    webView.setBackgroundColor(Color.argb(1, 0, 0, 0));
}

这篇关于WebView“闪烁"如果启用硬件加速,则为白色背景(Android 3.0+)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆