当我们在使用WebView时,如果加载的网友比较大,这加载速度将非常慢。
现总结几种加速WebView加载的方法
1、提高渲染的优先级
webView.getSettings().setRenderPriority(RenderPriority.HIGH);
2、使用webView.getSettings().setBlockNetworkImage,把图片加载放在最后来加载渲染。参照示例1.
3,使用硬件加速,该功能在Android 3.0 (API level 11)才加入。具体参照:http://developer.android.com/guide/topics/graphics/hardware-accel.html
示例1:
- package com.robin;
- import com.robin.R;
- import android.app.Activity;
- import android.app.Dialog;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.view.View;
- import android.view.Window;
- import android.webkit.DownloadListener;
- import android.webkit.WebChromeClient;
- import android.webkit.WebSettings;
- import android.webkit.WebSettings.RenderPriority;
- import android.webkit.WebView;
- import android.widget.Button;
- import android.widget.Toast;
- /**
- * @author Administrator
- *
- */
- public class MyActivity extends Activity {
- private WebView webView;
- final static String TAG = "MyActivity";
- Handler handler = new Handler();
- boolean blockLoadingNetworkImage=false;
- //static long t=0;
- //static long t1=0;
- String link;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- link = getIntent().getStringExtra("url");
- setContentView(R.layout.news_details);
- webView = (WebView) findViewById(R.id.webView);
- webView.getSettings().setBuiltInZoomControls(true);
- webView.getSettings().setJavaScriptEnabled(true);
- webView.getSettings().setRenderPriority(RenderPriority.HIGH);
- webView.getSettings().setBlockNetworkImage(true);
- blockLoadingNetworkImage=true;
- webView.setWebChromeClient(new WebChromeClient() {
- public void onProgressChanged(WebView view, int progress) {
- // Activities and WebViews measure progress with different
- // scales.
- // The progress meter will automatically disappear when we reach
- // 100%
- //Log.i(TAG, "progress:" + progress);
- if(loadingProgressDialog!=null&&loadingProgressDialog.isShowing())
- loadingProgressDialog.setProgress(progress);
- if (progress >= 100) {
- /*if(t==0)
- t=System.currentTimeMillis()-t1;
- else
- t=(t+System.currentTimeMillis()-t1)>>1;
- t1=System.currentTimeMillis()-t1;
- Log.i(TAG, "t:" + t/1000+" t1:"+t1/1000);*/
- if(blockLoadingNetworkImage)
- {
- webView.getSettings().setBlockNetworkImage(false);
- blockLoadingNetworkImage=false;
- }
- if (loadingProgressDialog!=null&&loadingProgressDialog.isShowing())
- dismissDialog(PROGRESS_DIALOG_CONNECTING);
- }
- }
- });
- Runnable r = new Runnable() {
- public void run() {
- webView.loadUrl(link);
- //t1=System.currentTimeMillis();
- Log.i(TAG, "url:" + link);
- showDialog(PROGRESS_DIALOG_CONNECTING);
- }
- };
- handler.postDelayed(r, 200);
- }
- protected void onResume() {
- super.onResume();
- if (webView.getProgress() < 100)
- showDialog(PROGRESS_DIALOG_CONNECTING);
- }
- protected void onDestroy() {
- webView.stopLoading();
- webView.destroy();
- super.onDestroy();
- }
- final static int PROGRESS_DIALOG_CONNECTING = 1000;
- ProgressDialog loadingProgressDialog = null;
- @Override
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case PROGRESS_DIALOG_CONNECTING: {
- ProgressDialog progressDialog = new ProgressDialog(this);
- progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- progressDialog.setMessage(getResources()
- .getString(R.string.loading));
- loadingProgressDialog = progressDialog;
- return progressDialog;
- }
- default:
- break;
- }
- return null;
- }
- protected void onPrepareDialog(int id, Dialog dialog) {
- super.onPrepareDialog(id, dialog);
- switch (id) {
- case PROGRESS_DIALOG_CONNECTING: {
- loadingProgressDialog.setMax(100);
- dialog.show();
- }
- break;
- default:
- break;
- }
- }
- }