【发布时间】:2015-06-03 15:43:28
【问题描述】:
出于性能考虑,我将 LAYER_TYPE_HARDWARE 用于我的 WebView。但它不能正常工作。
这是我的代码:
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>
当我使用View.LAYER_TYPE_SOFTWARE 时,一切正常。但是,当我切换到View.LAYER_TYPE_HARDWARE 时,出现了一些问题:
- 点击“加载文本”按钮,WebView 什么都没有显示,只是点击进入网页,然后显示其内容。
在mWebView.loadDataWithBaseURL之后插入mWebView.scrollBy(0, 1);可以解决这个问题,但我认为这只是一个“备份计划”
启动应用程序并快速单击“加载文本”按钮,结果将显示。但是之后点击按钮仍然会导致显示空白页 - “更改大小”功能在上述两种情况下都可以正常工作,但有时不起作用,我需要点击WebView区域才能显示内容。
- 我正在写一个类似的 WebView 类(代码here),但问题是相反的,页面总是在调用
loadDataWithBaseURL后显示(也使用 LAYER_TYPE_HARDWARE),但是调用了 javascript 函数(类似于更改大小)不更改 WebView 的内容,...
那么,我需要配置与 LAYER_TYPE_HARDWARE 一起使用吗?
【问题讨论】:
-
你找到解决这个问题的方法了吗?
标签: java android webview layer