Try to search your question here, if you can't find : Ask Any Question Now ?

Android WebView with layer_type_software not showing HTML5 canvas content

HomeCategory: stackoverflowAndroid WebView with layer_type_software not showing HTML5 canvas content
Avatargaurav asked 2 weeks ago

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?

When I leave out the following line of Java code:
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Then the Content is displayed correctly.

Java code:

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 file 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>

Expected result:
Red linear gradient in black border
https://heiri-web.ch/Gradient.png

Actual result:
Empty black border
https://heiri-web.ch/Empty.png

1 Answers
Best Answer
AvatarMatthias answered 2 weeks ago
Your Answer

8 + 4 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com