編輯:Android開發實例
當我們在使用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;
- }
- }
- }
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放
本文實例講述了Android編程實現ImageView圖片拋物線動畫效果的方法。分享給大家供大家參考,具體如下: 想實現拋物線動畫,必須知道拋物線的方程,這時候數
介紹 最近用淘寶客戶端的時候,編輯地址的時候有個地區選擇的功能。看上面的效果覺得挺酷,滾動的時候,是最後一個從下面飛上來挨著前一個。就自己鼓搗一個
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩