Android仿微信公眾號文章頁面加載進(jìn)度條
前言:
微信公眾號文章詳情頁面加載的時候,WebView會在頭部顯示一個進(jìn)度條,這樣做的好處就是用戶可以一邊加載網(wǎng)頁內(nèi)容的同時也可瀏覽網(wǎng)頁內(nèi)容,不需要等完全加載完之后才全部顯示出來。如何實(shí)現(xiàn)呢? 其實(shí)很簡單,自定義一個WebView就可以實(shí)現(xiàn)了。
詳細(xì)實(shí)現(xiàn)步驟如下 :
1、自定義一個ProgressWebView 繼續(xù) Webview
@SuppressWarnings("deprecation")
public class ProgressWebView extends WebView {
private ProgressBar progressbar;
public ProgressWebView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
progressbar = new ProgressBar(context, null,
android.R.attr.progressBarStyleHorizontal);
progressbar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
6, 0, 0));
progressbar.setProgressDrawable(this.getResources().getDrawable(
R.drawable.btn_progress_webview));
addView(progressbar);
setWebChromeClient(new WebChromeClient());
}
public ProgressWebView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public class WebChromeClient extends android.webkit.WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
progressbar.setVisibility(GONE);
} else {
if (progressbar.getVisibility() == GONE)
progressbar.setVisibility(VISIBLE);
progressbar.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
LayoutParams lp = (LayoutParams) progressbar.getLayoutParams();
lp.x = l;
lp.y = t;
progressbar.setLayoutParams(lp);
super.onScrollChanged(l, t, oldl, oldt);
}
}
2、設(shè)置R.drawable.btn_progress_webview 進(jìn)度條的顏色值:
<?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 設(shè)置背景色(黑色) --> <item android:id="@android:id/background"> <shape> <!-- 進(jìn)度條的四個棱角大小 0 為都是直角 隨著值的增大角越圓滑 --> <corners android:radius="0dip" /> <gradient android:endColor="#c0c0c0" android:startColor="#c0c0c0" /> </shape> </item> <!-- 設(shè)置進(jìn)度條顏色(綠色) --> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="0dip" /> <gradient android:endColor="#a13864" android:startColor="#a13864" /> </shape> </clip> </item> </layer-list>
3、在布局文件是如何使用呢?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.summer.progresswebview.MainActivity" > <com.summer.progresswebview.ProgressWebView android:id="@+id/progresswebview" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </RelativeLayout>
4、在Activity中是如何使用 和顯示網(wǎng)頁內(nèi)容的 :
public class MainActivity extends Activity {
private ProgressWebView progresswebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
progresswebview = (ProgressWebView) findViewById(R.id.progresswebview);
progresswebview.getSettings()
.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
progresswebview.getSettings().setJavaScriptEnabled(true);
progresswebview.getSettings().setSupportZoom(true);
progresswebview.getSettings().setLoadWithOverviewMode(true);
progresswebview.getSettings().setUseWideViewPort(true);
progresswebview.setVerticalScrollBarEnabled(false);
progresswebview.setHorizontalScrollBarEnabled(false);// 水平不顯示
progresswebview.getSettings().setBuiltInZoomControls(true); // 支持頁面放大縮小按鈕
progresswebview.setWebViewClient(client);
progresswebview.loadUrl("https://www.baidu.com/"); // 加載百度首頁網(wǎng)址
}
private WebViewClient client = new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progresswebview.getSettings().setLoadsImagesAutomatically(true);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//調(diào)用撥號程序
if (url.startsWith("mailto:") || url.startsWith("geo:") ||url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}else
view.loadUrl(url);
return true;
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
}
};
}
通過這幾個步驟,就是實(shí)現(xiàn)跟微信公眾號文章詳情頁顯示的進(jìn)度條一致了。
效果圖:


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)圖片加載進(jìn)度提示
- Android 進(jìn)度條 ProgressBar的實(shí)現(xiàn)代碼(隱藏、出現(xiàn)、加載進(jìn)度)
- Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- Android自定義View實(shí)現(xiàn)加載進(jìn)度條效果
- Android自定義View仿華為圓形加載進(jìn)度條
- Android基于Glide v4.x的圖片加載進(jìn)度監(jiān)聽
- Android Material加載進(jìn)度條制作代碼
- Android自定義View基礎(chǔ)開發(fā)之圖片加載進(jìn)度條
- Android Webview添加網(wǎng)頁加載進(jìn)度條實(shí)例詳解
- Android實(shí)現(xiàn)兩圓點(diǎn)之間來回移動加載進(jìn)度
相關(guān)文章
Android編程自定義Notification實(shí)例分析
這篇文章主要介紹了Android編程自定義Notification的用法,結(jié)合實(shí)例形式簡單分析了自定義Notification的具體功能與實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-12-12
Android自定義引導(dǎo)玩轉(zhuǎn)ViewPager的方法詳解
這篇文章主要給大家介紹了關(guān)于Android自定義引導(dǎo)玩轉(zhuǎn)ViewPager的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Android種使用Notification實(shí)現(xiàn)通知管理以及自定義通知欄實(shí)例(示例四)
本篇文章主要介紹了Android種使用Notification實(shí)現(xiàn)通知管理以及自定義通知欄實(shí)例,具有一定的參考價值,需要的朋友可以了解一下。2016-12-12
解析Android截取手機(jī)屏幕兩種實(shí)現(xiàn)方案
這篇文章主要介紹了解析Android截取手機(jī)屏幕兩種實(shí)現(xiàn)方案,非常具有實(shí)用價值,需要的朋友可以參考下2017-04-04
淺談AnDroidDraw+DroidDraw實(shí)現(xiàn)Android程序UI設(shè)計的分析說明
本篇文章是對AnDroidDraw+DroidDraw實(shí)現(xiàn)Android程序UI設(shè)計進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android獲取應(yīng)用程序大小和緩存的實(shí)例代碼
這篇文章主要介紹了Android獲取應(yīng)用程序大小和緩存的實(shí)例代碼的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-10-10
Android實(shí)現(xiàn)定時任務(wù)及鬧鐘
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)定時任務(wù)及鬧鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06
Android 消息機(jī)制以及handler的內(nèi)存泄露
這篇文章主要介紹了Android 消息機(jī)制以及handler的內(nèi)存泄露的相關(guān)資料,需要的朋友可以參考下2016-09-09
Android使用Canvas?2D實(shí)現(xiàn)循環(huán)菜單效果
循環(huán)菜單有很多種自定義方式,我們可以利用ViewPager或者RecyclerView?+?CarouselLayoutManager?或者RecyclerView?+?PageSnapHelper來實(shí)現(xiàn)這種效果,今天我們使用Canvas?2D來實(shí)現(xiàn)這種效果,感興趣的朋友可以參考下2024-01-01

