Android自定義帶進度條WebView仿微信加載過程
在正常開發(fā)中,我們客戶端需要用webView加載網(wǎng)頁,再遇到網(wǎng)絡慢或者訪問的服務器響應時,頁面是空白的,所以為了用戶更好的體驗,我們可以提供一個正在加載的進度條,提示用戶正在加載。
本文結(jié)構(gòu):
1、自定義webView
2、在應用中的使用
3、效果展示
一、自定義webView
1、首先定義一個類,繼承webView,并首先構(gòu)造方法
public class ProgressBarWebView extends WebView{}
自定義控件,先實現(xiàn)構(gòu)造方法,
第一中是程序內(nèi)部實例化采用,傳入context
public ProgressBarWebView(Context context) { super(context); }
第二種用于layout實例化,會把xml的參數(shù)通過AttributeSet帶入View內(nèi)
public ProgressBarWebView(Context context, AttributeSet attrs) { super(context, attrs); }
第三種主題的style信息,也從XML帶入
public ProgressBarWebView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }
而我們需要加載進度條布局,所以我們需要在第二中構(gòu)造方法中進行操作,如下:
//首選創(chuàng)建一個進度條,我們這里創(chuàng)建的是一個橫向的進度條 progressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal); //設置該進度條的位置參數(shù) progressBar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 10, 0, 0)); //我們想要設置該進度條的背景樣式 Drawable drawable = context.getResources().getDrawable(R.drawable.progress_bar_states); //設置背景樣式 progressBar.setProgressDrawable(drawable); //調(diào)用本身的addView(其實是調(diào)用ViewManager里的方法,看源碼)方法講進度條添加到當前布局視圖中 addView(progressBar); //正常想獲取或這進行交互一般要實現(xiàn)一下兩個方法,Myweblient()可以限制不用手機本身的瀏覽器,MyChromeClient()可以獲得網(wǎng)頁加載的進度,title等 setWebViewClient(new Myweblient()); setWebChromeClient(new MyChromeClient()); //是否可以縮放 getSettings().setSupportZoom(true); getSettings().setBuiltInZoomControls(true);
2、重寫WebViewClient,設置再本身的webview打開,不調(diào)用系統(tǒng)的瀏覽器:
//需要自己設置要不會打開手機瀏覽器 private class Myweblient extends WebViewClient{ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }
3、重寫WebChromeClient,獲取相應進度信息,并設置
private class MyChromeClient extends WebChromeClient{ @Override public void onProgressChanged(WebView view, int newProgress) { if (newProgress == 100) { //當網(wǎng)頁全部加載完畢時 progressBar.setVisibility(GONE); } else { if (progressBar.getVisibility() == GONE) progressBar.setVisibility(VISIBLE); progressBar.setProgress(newProgress); } super.onProgressChanged(view, newProgress); } }
4、前文構(gòu)造器我們提到的進度條背景R.drawable.progress_bar_states,需要再xml中定義;
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 進度條背景 --> <item android:id="@android:id/background"> <shape> <corners android:radius="2dp" /> <gradient android:angle="270" android:centerColor="#E3E3E3" android:endColor="#E6E6E6" android:startColor="#C8C8C8" /> </shape> </item> <!-- 綠色的進度值 --> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="2dp" /> <gradient android:centerColor="#4AEA2F" android:endColor="#31CE15" android:startColor="#5FEC46" /> </shape> </clip> </item> </layer-list>
二、在頁面中的使用
//布局中 <com.example.videodemo.ProgressBarWebView android:id="@+id/ss" android:layout_width="match_parent" android:layout_height="match_parent"/>
Activity中使用
ProgressBarWebView webView=(ProgressBarWebView) findViewById(R.id.ss); webView.loadUrl("http://www.baidu.com/");
三、最終效果
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
深入Understanding Android ContentProvider詳解
本篇文章是對Android ContentProvider進行了詳細的分析介紹,需要的朋友參考下2013-05-05Android通訊錄開發(fā)之刪除功能的實現(xiàn)方法
這篇文章主要介紹了Android通訊錄開發(fā)之刪除功能的實現(xiàn)方法,有需要的朋友可以參考一下2014-01-01Android自定義Spinner下拉列表(使用ArrayAdapter和自定義Adapter實現(xiàn))
這篇文章主要介紹了Android自定義Spinner下拉列表(使用ArrayAdapter和自定義Adapter實現(xiàn))的相關資料,需要的朋友可以參考下2015-10-10