Reputation: 2350
For performance perposes, I'm using LAYER_TYPE_HARDWARE for my WebView. But it doesn't work correctly.
This is my code:
SmoothWebViewActivity.java
package uet.ducvu.androidexample;
import java.io.InputStream;
import java.util.Scanner;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class SmoothWebViewActivity extends Activity {
private WebView mWebView;
private int mTextSize = 15;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smooth_web_view);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
mWebView = (WebView) findViewById(R.id.my_web_view);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.getSettings().setJavaScriptEnabled(true);
//mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
public void loadText(View view){
InputStream raw = getResources().openRawResource(R.raw.my_webview_template);
Scanner scanner = new Scanner(raw).useDelimiter("\\A");
String core = scanner.next();
String body = "Some data here";
scanner.close();
for (int i = 0; i < 1000; i++)
body += "<br/>" + i;
String html = String.format(core, mTextSize, body);
mWebView.loadDataWithBaseURL(null, html, null, "utf-8", null);
System.out.println("Hardware: " + mWebView.isHardwareAccelerated());
}
public void changeSize(View view){
mTextSize++;
mWebView.loadUrl("javascript:dynamicChangeSize(" + mTextSize + ");");
}
}
activity_smooth_web_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="uet.ducvu.androidexample.SmoothWebViewActivity"
tools:ignore="HardcodedText,ButtonStyle" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="loadText"
android:text="Load text" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="changeSize"
android:text="Change size" />
</LinearLayout>
<WebView
android:id="@+id/my_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
res/raw/my_webview_template.html
<html>
<head>
</head>
<body style="font-size:%dpx;">%s</body>
<script type="text/javascript">
function dynamicChangeSize(size){
document.body.style.fontSize = size + "px";
}
</script>
</html>
AndroidManifest.xml
<activity
android:name=".SmoothWebViewActivity"
android:label="@string/title_activity_smooth_web_view"
android:hardwareAccelerated="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
When i use View.LAYER_TYPE_SOFTWARE
, everything works well. But, when i switch to View.LAYER_TYPE_HARDWARE
, there are some problems:
mWebView.scrollBy(0, 1);
after mWebView.loadDataWithBaseURL
can solve this problem, but i think this is a "backup plan" onlyloadDataWithBaseURL
(use LAYER_TYPE_HARDWARE as well), but a call to javascript function (similar to Change size) not change the content of the WebView,...
Upvotes: 0
Views: 5812
Reputation: 607
This is an old question, but I noticed that you have turned on hardware acceleration at the
activity level in your manifest,
the window level using WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
,
and the View level using mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
. Try only using the View level acceleration, or just the android:hardwareAccelerated="true"
in the manifest. Not both, together.
From the Android developers page...
There are two different ways to check whether the application is hardware accelerated:
View.isHardwareAccelerated() returns true if the View is attached to a hardware accelerated window. Canvas.isHardwareAccelerated() returns true if the Canvas is hardware accelerated
I hope this helps someone.
Upvotes: 2