Android如何實(shí)現(xiàn)自定義帶進(jìn)度條WebView仿微信加載

這篇文章主要介紹Android如何實(shí)現(xiàn)自定義帶進(jìn)度條WebView仿微信加載,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

成都創(chuàng)新互聯(lián)公司成立與2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元新田做網(wǎng)站,已為上家服務(wù),為新田各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220

本文結(jié)構(gòu):

1、自定義webView
2、在應(yīng)用中的使用
3、效果展示

一、自定義webView

1、首先定義一個(gè)類,繼承webView,并首先構(gòu)造方法

public class ProgressBarWebView extends WebView{}

自定義控件,先實(shí)現(xiàn)構(gòu)造方法,
第一中是程序內(nèi)部實(shí)例化采用,傳入context

public ProgressBarWebView(Context context) {
 super(context);
 }

第二種用于layout實(shí)例化,會(huì)把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);
 }

而我們需要加載進(jìn)度條布局,所以我們需要在第二中構(gòu)造方法中進(jìn)行操作,如下:

//首選創(chuàng)建一個(gè)進(jìn)度條,我們這里創(chuàng)建的是一個(gè)橫向的進(jìn)度條
progressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal); 
//設(shè)置該進(jìn)度條的位置參數(shù)
progressBar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 10, 0, 0)); 
//我們想要設(shè)置該進(jìn)度條的背景樣式 
Drawable drawable = context.getResources().getDrawable(R.drawable.progress_bar_states);
//設(shè)置背景樣式 
progressBar.setProgressDrawable(drawable); 
//調(diào)用本身的addView(其實(shí)是調(diào)用ViewManager里的方法,看源碼)方法講進(jìn)度條添加到當(dāng)前布局視圖中
addView(progressBar); 
//正常想獲取或這進(jìn)行交互一般要實(shí)現(xiàn)一下兩個(gè)方法,Myweblient()可以限制不用手機(jī)本身的瀏覽器,MyChromeClient()可以獲得網(wǎng)頁(yè)加載的進(jìn)度,title等
setWebViewClient(new Myweblient()); 
setWebChromeClient(new MyChromeClient()); 
//是否可以縮放 
getSettings().setSupportZoom(true); 
getSettings().setBuiltInZoomControls(true);

2、重寫WebViewClient,設(shè)置再本身的webview打開,不調(diào)用系統(tǒng)的瀏覽器:

//需要自己設(shè)置要不會(huì)打開手機(jī)瀏覽器
 private class Myweblient extends WebViewClient{
 @Override
 public boolean shouldOverrideUrlLoading(WebView view, String url) {
  view.loadUrl(url);
  return true;
 }
 }

3、重寫WebChromeClient,獲取相應(yīng)進(jìn)度信息,并設(shè)置

private class MyChromeClient extends WebChromeClient{
 @Override 
 public void onProgressChanged(WebView view, int newProgress) { 
  if (newProgress == 100) { //當(dāng)網(wǎng)頁(yè)全部加載完畢時(shí)
  progressBar.setVisibility(GONE); 
  } else { 
  if (progressBar.getVisibility() == GONE) 
   progressBar.setVisibility(VISIBLE); 
  progressBar.setProgress(newProgress); 
  } 
  super.onProgressChanged(view, newProgress); 
 } 

 }

4、前文構(gòu)造器我們提到的進(jìn)度條背景R.drawable.progress_bar_states,需要再xml中定義;

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
 <!-- 進(jìn)度條背景 -->
 <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> 
 <!-- 綠色的進(jìn)度值 -->
 <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>

二、在頁(yè)面中的使用

//布局中
<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/");

三、最終效果

Android如何實(shí)現(xiàn)自定義帶進(jìn)度條WebView仿微信加載

Android如何實(shí)現(xiàn)自定義帶進(jìn)度條WebView仿微信加載

以上是“Android如何實(shí)現(xiàn)自定義帶進(jìn)度條WebView仿微信加載”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當(dāng)前標(biāo)題:Android如何實(shí)現(xiàn)自定義帶進(jìn)度條WebView仿微信加載
本文網(wǎng)址:http://muchs.cn/article14/jchpge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)靜態(tài)網(wǎng)站、微信小程序、網(wǎng)站建設(shè)、品牌網(wǎng)站制作動(dòng)態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作