的WebView"闪烁"白色背景,如果启用硬件加速(安卓3.0+)白色、背景、硬件加速、WebView

2023-09-03 22:08:35 作者:ㄨ东京心跳。樱树抽芽

我有一个问题与web视图(安卓3.0+),其中的WebView始终显示一个白色的背景前显示我黑色背景(闪烁)。这里是我的简单测试code:

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=\"https://m.xsw88.com/allimgs/daicuo/20230903/5759.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.

这已杰利贝恩得到解决,但也有一些用户报告在奇巧看到这一点。检查您是否具有的没有的禁用硬件加速,如果问题真的消失了,你可能会想包装在一个条件语句code,只针对较旧的设备:

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