Android自定義ProgressBar實現(xiàn)漂亮的進(jìn)度提示框
在android智能平板設(shè)備應(yīng)用中,一項耗時的操作總要有個提示進(jìn)度的框來提高用戶的操作體驗,操作進(jìn)度提示框就顯得很常用了。
系統(tǒng)自帶的有進(jìn)度條ProgressBar,一般用于顯示一個過程,例如數(shù)據(jù)加載過程,文件下載進(jìn)度,音樂播放進(jìn)度等。但是樣式太單一不好看,因此有必要自定義一個方便使用。
以下記錄下封裝的進(jìn)度展示對話框ProgressDialog。
先來展示下效果圖:
需要準(zhǔn)備好素材。如上圖中的那個旋轉(zhuǎn)的圈圈,素材圖是一張png圖片,分辨率114x114:
如何實現(xiàn)自動旋轉(zhuǎn)的效果呢,使用android的Rotate動畫。
在res/drawable下建一個rotate_dialog_progress.xml文件,內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:drawable="@drawable/loading_white" ? ? android:fromDegrees="0" ? ? android:interpolator="@android:anim/cycle_interpolator" ? ? android:pivotX="50%" ? ? android:pivotY="50%" ? ? android:toDegrees="1440" />
這里面的幾個屬性解釋:
<?xml version="1.0" encoding="utf-8"?> ?<rotate xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:fromDegrees="0" ? ? ? ? ? ?#初始角度 ? ? android:toDegrees="1440" ? ? ? ? ? #結(jié)束時角度,值為正時順時針旋轉(zhuǎn),值為負(fù)時逆時針旋轉(zhuǎn) ? ? android:pivotX="50%" ? ? ? ? ? ? ? #旋轉(zhuǎn)中心x軸坐標(biāo),取值可以是數(shù)值(50)、百分?jǐn)?shù)(50%)、百 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 分?jǐn)?shù)p(50%p),當(dāng)取值為數(shù)值時,縮放起點為View左上角坐標(biāo) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 加具體數(shù)值像素,當(dāng)取值為百分?jǐn)?shù)時,表示在當(dāng)前View左上角坐 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 加上View寬度的具體百分比,當(dāng)取值為百分?jǐn)?shù)p時,表示在View ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 左上角坐標(biāo)加上父控件寬度的具體百分比 ? ? android:pivotY="50%" ? ? ? ? ? ? ? #同上 ? ? android:duration="700" ? ? ? ? ? ? #動畫持續(xù)時間,毫秒為單位 ? ? android:fillAfter="true" ? ? ? ? ? #動畫結(jié)束后,保持結(jié)束時的狀態(tài) ? ? android:fillBefore="true" ? ? ? ? ?#動畫結(jié)束后,恢復(fù)為初始狀態(tài) ? ? android:fillEnabled="true" ? ? ? ? #效果同上 ? ? android:repeatCount="5" ? ? ? ? ? ?#重復(fù)次數(shù),取值為-1時無限重復(fù),默認(rèn)動畫執(zhí)行一次 ? ? android:repeatMode ="reverse" ? ? ?#重復(fù)模式,有reverse和restart兩個值,前者為倒序回放,后者為重新開始 ? ? android:interpolator="@android:anim/accelerate_decelerate_interpolator" #插值器 ? ? ? ? ? ? ? ? />
接下來在styles.xml文件中定義一個樣式文件供使用。內(nèi)容如下:
<style name="myProgressBarStyleLarge"> ? ? ? ? <item name="android:indeterminateDrawable">@drawable/rotate_dialog_progress</item> ? ? ? ? <item name="android:width">200dp</item> ? ? ? ? <item name="android:height">200dp</item> ? ? </style>
然后就可以這樣使用我們自定義的progressbar啦:
<ProgressBar ? ? ? ? android:id="@+id/loadingImageView" ? ? ? ? style="@style/myProgressBarStyleLarge" ? ? ? ? android:layout_width="200dp" ? ? ? ? android:layout_height="200dp" ? ? ? ? android:layout_gravity="center" ? ? ? ? android:padding="20dp" />
這還不算完,一般progressbar要放在dialog對話框中來用。看下對框框dialog的樣式dialog_progress.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="wrap_content" ? ? android:background="@drawable/corner_bg_dialog_progress" ? ? android:orientation="vertical" ? ? android:gravity="center" ? ? android:paddingTop="30dp" ? ? android:paddingBottom="30dp" ? ? android:paddingLeft="30dp" ? ? android:paddingRight="30dp"> ? ? ? <ProgressBar ? ? ? ? android:id="@+id/loadingImageView" ? ? ? ? style="@style/myProgressBarStyleLarge" ? ? ? ? android:layout_width="200dp" ? ? ? ? android:layout_height="200dp" ? ? ? ? android:layout_gravity="center" ? ? ? ? android:padding="20dp" /> ? ? ? <TextView ? ? ? ? android:id="@+id/loadingmsg" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_gravity="center" ? ? ? ? android:textColor="@android:color/white" ? ? ? ? android:text="正在處理中..." ? ? ? ? android:textSize="30dp" ? ? ? ? android:layout_marginBottom="30dp" ? ? ? ? android:layout_marginTop="10dp" /> ? </LinearLayout>
為了使Dialog的背景和邊框的棱角好看,這里自定義了Dialog的背景。
android:background="@drawable/corner_bg_dialog_progress"
它就是一個放在res/drawable文件夾下的一個自定義shape。
corner_bg_dialog_progress.xml文件內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?> <shape android:shape="rectangle" ? ? xmlns:android="http://schemas.android.com/apk/res/android"> ? ? <solid android:color="@color/light_black" /> ? ? <corners android:radius="@dimen/corner_radius_one" /> </shape>
最后,實現(xiàn)一個Dialog并加載這個dialog_progress.xml布局,顯示出來即可。
在需要提示進(jìn)度的地方,showProgressDialog。在結(jié)束時closeProgressDialog。
override fun showProgressDialog(msg: String) { ? ? ? ? ? ? ? ? if (dialogProgress == null) { ? ? ? ? ? ? ? ? ? ? dialogProgress = DialogProgress(mPresentation!!.context, activity.getString(R.string.loading), false) ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? dialogProgress!!.setMessage(msg) ? ? ? ? ? ? ? ? dialogProgress!!.show() ? ? ? ? ? ? } ? override fun closeProgressDialog() { ? ? ? ? ? ? ? ? dialogProgress?.dismiss() ? ? ? ? ? ? }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Activity之間相互調(diào)用與傳遞參數(shù)的原理與用法分析
這篇文章主要介紹了Android Activity之間相互調(diào)用與傳遞參數(shù)的原理與用法,較為詳細(xì)的分析了Android組件的構(gòu)成以及Activity的創(chuàng)建、調(diào)用、切換等相關(guān)操作技巧,需要的朋友可以參考下2016-08-08Android Studio升級到3.0 Terminal 中文顯示異常解決
本篇文章主要介紹了Android Studio升級到3.0 Terminal 中文顯示異常解決,非常具有實用價值,需要的朋友可以參考下2017-10-10Android webview和js互相調(diào)用實現(xiàn)方法
這篇文章主要介紹了 Android webview和js互相調(diào)用實現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2016-10-10Android熱更新開源項目Tinker集成實踐總結(jié)
最近項目集成了Tinker,開始認(rèn)為集成會比較簡單,但是在實際操作的過程中還是遇到了一些問題,本文就會介紹在集成過程大家基本會遇到的主要問題。下面跟著小編一起來看下吧2017-01-01Android監(jiān)聽手機電話狀態(tài)與發(fā)送郵件通知來電號碼的方法(基于PhoneStateListene實現(xiàn))
這篇文章主要介紹了Android監(jiān)聽手機電話狀態(tài)與發(fā)送郵件通知來電號碼的方法,通過Android的PhoneStateListene實現(xiàn)該功能,需要的朋友可以參考下2016-01-01