WebView簡介(加速加載篇)
阿新 • • 發佈:2019-03-03
sdi iss sage 渲染 mat find auth null gets
當我們在使用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;
}
}
}
再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!http://www.captainbed.net
WebView簡介(加速加載篇)