Android webview加载数据性能很慢
Android webview loading data performance very slow
您好,我正在开发一个应用程序,因为我正在使用 Android WebView。
每当我启动 webview activity,从 test.txt 文件加载字符串 html 格式的数据。
test.txt文件包含将近2.5MB的数据,加载test.txt文件后,如果滑动屏幕结束读取所有数据并按返回。
随后启动 webview activity 需要更多时间来呈现数据。在首次启动 webview 时,它花费的时间最少。
我在启动此 activity.
期间没有收到任何 error/exception/crash
我正在使用 Android API 18 级及以上
注意:我对这个问题做了很多研究。我试过 Android webview slow and Android WebView performance 的解决方案没用。
我做不到 android:hardwareAccelerated="true" <-- 为什么因为它有很多副作用(我还是用了这个,但我在这个问题上没有发现任何变化)。
我不会用
webview.getSettings().setRenderPriority(RenderPriority.HIGH);
setRenderPriority() --> 此方法已在 API 级别 18 中弃用。
不建议调整线程优先级,后续版本将不支持。
我的DataLoader.javaclass
public class DataLoader extends Activity {
private WebView webView = null;
// Progress Dialog
private ProgressDialog progressDialog;
String dataContent = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
webView = (WebView)findViewById(R.id.text);
// Loading webview in Background Thread
new LoadWebView().execute();
}
class LoadWebView extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(DataLoader.this);
progressDialog.setTitle("Loading...");
progressDialog.setMessage("Please wait.");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
}
protected String doInBackground(String... args) {
// AssetFileReader read the txt file and return data in the form of string
dataContent = AssetFileReader.read(getApplicationContext(), "test.txt");
return null;
}
protected void onPostExecute(String str) {
// dismiss the dialog
progressDialog.dismiss();
if (dataContent != null) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
WebSettings s = webView.getSettings();
s.setUseWideViewPort(true);
s.setSupportZoom(true);
s.setBuiltInZoomControls(true);
s.setDisplayZoomControls(false);
s.setJavaScriptEnabled(true);
webView.loadData(dataContent, "text/html", "utf-8");
}
});
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
webView.clearCache(true);
webView.clearHistory();
}
}
我的web_view.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/text">
</WebView>
</LinearLayout>
如果您对此问题有任何解决办法,请告诉我。
我认为以下效果最好:
if (Build.VERSION.SDK_INT >= 19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
Android 19 具有用于 WebView 的 Chromium 引擎。我想它在硬件加速下效果更好。
更多信息Android 4.4 KitKat, the browser and the Chrome WebView
Hardware Acceleration 也可以 trick.You 可以在您的应用程序中的不同级别使用它。
应用级别
<application android:hardwareAccelerated="true" ...>
Activity级
<application android:hardwareAccelerated="true">
<activity ... />
<activity android:hardwareAccelerated="false" />
</application>
Window级
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
查看级别
myView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
但是正如你的问题中已经提到的,你能详细说明一下它的副作用吗?
我试过下面的代码,它对我来说工作得更快
// For API level below 18 (This method was deprecated in API level 18)
webview.getSettings().setRenderPriority(RenderPriority.HIGH);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
if (Build.VERSION.SDK_INT >= 19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
万一有人到这里还在用头撞墙试图让 WebView 渲染得更快,试试 Crosswalk's XWalkView。对我们的应用产生了巨大影响。
您好,我正在开发一个应用程序,因为我正在使用 Android WebView。 每当我启动 webview activity,从 test.txt 文件加载字符串 html 格式的数据。 test.txt文件包含将近2.5MB的数据,加载test.txt文件后,如果滑动屏幕结束读取所有数据并按返回。 随后启动 webview activity 需要更多时间来呈现数据。在首次启动 webview 时,它花费的时间最少。 我在启动此 activity.
期间没有收到任何 error/exception/crash我正在使用 Android API 18 级及以上
注意:我对这个问题做了很多研究。我试过 Android webview slow and Android WebView performance 的解决方案没用。
我做不到 android:hardwareAccelerated="true" <-- 为什么因为它有很多副作用(我还是用了这个,但我在这个问题上没有发现任何变化)。
我不会用 webview.getSettings().setRenderPriority(RenderPriority.HIGH);
setRenderPriority() --> 此方法已在 API 级别 18 中弃用。 不建议调整线程优先级,后续版本将不支持。
我的DataLoader.javaclass
public class DataLoader extends Activity {
private WebView webView = null;
// Progress Dialog
private ProgressDialog progressDialog;
String dataContent = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
webView = (WebView)findViewById(R.id.text);
// Loading webview in Background Thread
new LoadWebView().execute();
}
class LoadWebView extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(DataLoader.this);
progressDialog.setTitle("Loading...");
progressDialog.setMessage("Please wait.");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
}
protected String doInBackground(String... args) {
// AssetFileReader read the txt file and return data in the form of string
dataContent = AssetFileReader.read(getApplicationContext(), "test.txt");
return null;
}
protected void onPostExecute(String str) {
// dismiss the dialog
progressDialog.dismiss();
if (dataContent != null) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
WebSettings s = webView.getSettings();
s.setUseWideViewPort(true);
s.setSupportZoom(true);
s.setBuiltInZoomControls(true);
s.setDisplayZoomControls(false);
s.setJavaScriptEnabled(true);
webView.loadData(dataContent, "text/html", "utf-8");
}
});
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
webView.clearCache(true);
webView.clearHistory();
}
}
我的web_view.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/text">
</WebView>
</LinearLayout>
如果您对此问题有任何解决办法,请告诉我。
我认为以下效果最好:
if (Build.VERSION.SDK_INT >= 19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
Android 19 具有用于 WebView 的 Chromium 引擎。我想它在硬件加速下效果更好。
更多信息Android 4.4 KitKat, the browser and the Chrome WebView
Hardware Acceleration 也可以 trick.You 可以在您的应用程序中的不同级别使用它。
应用级别
<application android:hardwareAccelerated="true" ...>
Activity级
<application android:hardwareAccelerated="true">
<activity ... />
<activity android:hardwareAccelerated="false" />
</application>
Window级
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
查看级别
myView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
但是正如你的问题中已经提到的,你能详细说明一下它的副作用吗?
我试过下面的代码,它对我来说工作得更快
// For API level below 18 (This method was deprecated in API level 18)
webview.getSettings().setRenderPriority(RenderPriority.HIGH);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
if (Build.VERSION.SDK_INT >= 19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
万一有人到这里还在用头撞墙试图让 WebView 渲染得更快,试试 Crosswalk's XWalkView。对我们的应用产生了巨大影响。