当我们在使用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:
 
  1. package com.robin;  
  2.  
  3.  
  4. import com.robin.R;  
  5. import android.app.Activity;  
  6. import android.app.Dialog;  
  7. import android.app.ProgressDialog;  
  8. import android.content.Context;  
  9. import android.content.DialogInterface;  
  10. import android.os.Bundle;  
  11. import android.os.Handler;  
  12. import android.os.Message;  
  13. import android.util.Log;  
  14. import android.view.View;  
  15. import android.view.Window;  
  16. import android.webkit.DownloadListener;  
  17. import android.webkit.WebChromeClient;  
  18. import android.webkit.WebSettings;  
  19. import android.webkit.WebSettings.RenderPriority;  
  20. import android.webkit.WebView;  
  21. import android.widget.Button;  
  22. import android.widget.Toast;  
  23.  
  24.  
  25. /**  
  26.  * @author Administrator  
  27.  *   
  28.  */ 
  29. public class MyActivity extends Activity {  
  30. private WebView webView;  
  31. final static String TAG = "MyActivity";  
  32. Handler handler = new Handler();  
  33. boolean blockLoadingNetworkImage=false;  
  34. //static long t=0;  
  35. //static long t1=0;  
  36. String link;  
  37. protected void onCreate(Bundle savedInstanceState) {  
  38. super.onCreate(savedInstanceState);  
  39. link = getIntent().getStringExtra("url");  
  40. setContentView(R.layout.news_details);  
  41.  
  42.  
  43. webView = (WebView) findViewById(R.id.webView);  
  44. webView.getSettings().setBuiltInZoomControls(true);  
  45. webView.getSettings().setJavaScriptEnabled(true);  
  46. webView.getSettings().setRenderPriority(RenderPriority.HIGH);  
  47. webView.getSettings().setBlockNetworkImage(true);  
  48. blockLoadingNetworkImage=true;  
  49. webView.setWebChromeClient(new WebChromeClient() {  
  50. public void onProgressChanged(WebView view, int progress) {  
  51. // Activities and WebViews measure progress with different  
  52. // scales.  
  53. // The progress meter will automatically disappear when we reach  
  54. // 100%  
  55. //Log.i(TAG, "progress:" + progress);  
  56. if(loadingProgressDialog!=null&&loadingProgressDialog.isShowing())  
  57. loadingProgressDialog.setProgress(progress);  
  58. if (progress >= 100) {  
  59. /*if(t==0)  
  60. t=System.currentTimeMillis()-t1;  
  61. else  
  62. t=(t+System.currentTimeMillis()-t1)>>1;  
  63. t1=System.currentTimeMillis()-t1;  
  64. Log.i(TAG, "t:" + t/1000+" t1:"+t1/1000);*/ 
  65. if(blockLoadingNetworkImage)  
  66. {  
  67. webView.getSettings().setBlockNetworkImage(false);  
  68. blockLoadingNetworkImage=false;  
  69. }  
  70. if (loadingProgressDialog!=null&&loadingProgressDialog.isShowing())  
  71. dismissDialog(PROGRESS_DIALOG_CONNECTING);  
  72. }  
  73. }  
  74. });  
  75. Runnable r = new Runnable() {  
  76. public void run() {  
  77. webView.loadUrl(link);  
  78. //t1=System.currentTimeMillis();  
  79. Log.i(TAG, "url:" + link);  
  80. showDialog(PROGRESS_DIALOG_CONNECTING);  
  81. }  
  82. };  
  83. handler.postDelayed(r, 200);  
  84.  
  85.  
  86. }  
  87.  
  88.  
  89. protected void onResume() {  
  90. super.onResume();  
  91. if (webView.getProgress() < 100)  
  92. showDialog(PROGRESS_DIALOG_CONNECTING);  
  93. }  
  94.  
  95.  
  96. protected void onDestroy() {  
  97. webView.stopLoading();  
  98. webView.destroy();  
  99. super.onDestroy();  
  100. }  
  101.  
  102.  
  103. final static int PROGRESS_DIALOG_CONNECTING = 1000;  
  104. ProgressDialog loadingProgressDialog = null;  
  105.  
  106.  
  107. @Override 
  108. protected Dialog onCreateDialog(int id) {  
  109. switch (id) {  
  110. case PROGRESS_DIALOG_CONNECTING: {  
  111. ProgressDialog progressDialog = new ProgressDialog(this);  
  112. progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  113. progressDialog.setMessage(getResources()  
  114. .getString(R.string.loading));  
  115. loadingProgressDialog = progressDialog;  
  116. return progressDialog;  
  117. }  
  118. default:  
  119. break;  
  120. }  
  121. return null;  
  122. }  
  123.  
  124.  
  125. protected void onPrepareDialog(int id, Dialog dialog) {  
  126. super.onPrepareDialog(id, dialog);  
  127. switch (id) {  
  128. case PROGRESS_DIALOG_CONNECTING: {  
  129. loadingProgressDialog.setMax(100);  
  130. dialog.show();  
  131. }  
  132. break;  
  133. default:  
  134. break;  
  135. }  
  136. }  
  137.  
  138.  

來源:http://hunankeda110.iteye.com/blog/1807839

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()