带有layer_type_software的Android WebView不显示HTML5画布内容 [英] Android WebView with layer_type_software not showing HTML5 canvas content
问题描述
自上次更新Android WebView以来,通过将WebView的图层类型设置为View.LAYER_TYPE_SOFTWARE禁用硬件加速时,无法正确显示带有HTML5画布的网页.有没有解决此问题的方法?
Since last update of Android WebView a web page with a HTML5 canvas is not displayed correctly when hardware acceleration is disabled by setting the WebView's layer type to View.LAYER_TYPE_SOFTWARE. Is there a workaround for this problem?
当我省略以下Java代码行时: webView.setLayerType(View.LAYER_TYPE_SOFTWARE,null); 然后内容将正确显示.
When I leave out the following line of Java code: webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); Then the Content is displayed correctly.
WebView webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.loadUrl("file:///android_asset/Gradient.html");
HTML文件test.html:
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
// Create gradient
var grd = ctx.createLinearGradient(0,0,200,0);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
// Fill with gradient
ctx.fillStyle = grd;
ctx.fillRect(10,10,150,80);
</script>
</body>
</html>
预期结果: 黑色边框中的红色线性渐变 https://heiri-web.ch/Gradient.png
Expected result: Red linear gradient in black border https://heiri-web.ch/Gradient.png
实际结果: 空的黑色边框 https://heiri-web.ch/Empty.png
Actual result: Empty black border https://heiri-web.ch/Empty.png
推荐答案
提交错误报告后 https:向Google//issuetracker.google.com/issues/139276381 发送给Google,我收到的答复是,由于LAYER_TYPE_SOFTWARE
已过时,因此无法修复该错误.因为我需要LAYER_TYPE_SOFTWAR
E才能使用WebView.getDrawingCache()
获得完整的屏幕截图(包括HTML5画布内容),所以我要求一种替代方法.
遵循WebView开发人员团队成员Bo Liu的建议,我设法在不使用LAYER_TYPE_SOFTWARE
的情况下获得了WebView的完整屏幕截图:
创建一个ImageReader,使用ImageReader.getSurface()
获取一个Surface,在Surface上创建一个VirtualDisplay,使用VirtualDisplay.getDisplay()
获取一个Display,在其在Display上的布局中创建一个WebView的Presentation,使用Presentation.show()
,使用
After submitting a bug report https://issuetracker.google.com/issues/139276381 to Google I received the reply that the bug will not be fixed as LAYER_TYPE_SOFTWARE
is obsolete. Because I needed LAYER_TYPE_SOFTWAR
E to get a complete screenshot (including HTML5 canvas content) using WebView.getDrawingCache()
I asked for an alternative method.
Following the advice of Bo Liu, member of the WebView developers team, I managed to get a complete screenshot of a WebView without using LAYER_TYPE_SOFTWARE
:
Create an ImageReader, use ImageReader.getSurface()
to get a Surface, create a VirtualDisplay on the Surface, use VirtualDisplay.getDisplay()
to get a Display, create a Presentation with the WebView in its layout on the Display, use Presentation.show()
, use ImageReader.acquireLatestImage()
to read the rendered Image of WebView content.
这篇关于带有layer_type_software的Android WebView不显示HTML5画布内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!