Android自定義ProgressBar實(shí)現(xiàn)漂亮的進(jìn)度提示框
在android智能平板設(shè)備應(yīng)用中,一項(xiàng)耗時(shí)的操作總要有個(gè)提示進(jìn)度的框來提高用戶的操作體驗(yàn),操作進(jìn)度提示框就顯得很常用了。
系統(tǒng)自帶的有進(jìn)度條ProgressBar,一般用于顯示一個(gè)過程,例如數(shù)據(jù)加載過程,文件下載進(jìn)度,音樂播放進(jìn)度等。但是樣式太單一不好看,因此有必要自定義一個(gè)方便使用。
以下記錄下封裝的進(jìn)度展示對話框ProgressDialog。
先來展示下效果圖:

需要準(zhǔn)備好素材。如上圖中的那個(gè)旋轉(zhuǎn)的圈圈,素材圖是一張png圖片,分辨率114x114:

如何實(shí)現(xiàn)自動旋轉(zhuǎn)的效果呢,使用android的Rotate動畫。
在res/drawable下建一個(gè)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" />
這里面的幾個(gè)屬性解釋:
<?xml version="1.0" encoding="utf-8"?> ?<rotate xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:fromDegrees="0" ? ? ? ? ? ?#初始角度 ? ? android:toDegrees="1440" ? ? ? ? ? #結(jié)束時(shí)角度,值為正時(shí)順時(shí)針旋轉(zhuǎn),值為負(fù)時(shí)逆時(shí)針旋轉(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ù)值時(shí),縮放起點(diǎn)為View左上角坐標(biāo) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 加具體數(shù)值像素,當(dāng)取值為百分?jǐn)?shù)時(shí),表示在當(dāng)前View左上角坐 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 加上View寬度的具體百分比,當(dāng)取值為百分?jǐn)?shù)p時(shí),表示在View ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 左上角坐標(biāo)加上父控件寬度的具體百分比 ? ? android:pivotY="50%" ? ? ? ? ? ? ? #同上 ? ? android:duration="700" ? ? ? ? ? ? #動畫持續(xù)時(shí)間,毫秒為單位 ? ? android:fillAfter="true" ? ? ? ? ? #動畫結(jié)束后,保持結(jié)束時(shí)的狀態(tài) ? ? android:fillBefore="true" ? ? ? ? ?#動畫結(jié)束后,恢復(fù)為初始狀態(tài) ? ? android:fillEnabled="true" ? ? ? ? #效果同上 ? ? android:repeatCount="5" ? ? ? ? ? ?#重復(fù)次數(shù),取值為-1時(shí)無限重復(fù),默認(rèn)動畫執(zhí)行一次 ? ? android:repeatMode ="reverse" ? ? ?#重復(fù)模式,有reverse和restart兩個(gè)值,前者為倒序回放,后者為重新開始 ? ? android:interpolator="@android:anim/accelerate_decelerate_interpolator" #插值器 ? ? ? ? ? ? ? ? />
接下來在styles.xml文件中定義一個(gè)樣式文件供使用。內(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對話框中來用??聪聦蚩騞ialog的樣式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"
它就是一個(gè)放在res/drawable文件夾下的一個(gè)自定義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>
最后,實(shí)現(xiàn)一個(gè)Dialog并加載這個(gè)dialog_progress.xml布局,顯示出來即可。
在需要提示進(jìn)度的地方,showProgressDialog。在結(jié)束時(shí)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)文章
RelativeLayout(相對布局)用法實(shí)例講解
在本文里小編給大家分享了關(guān)于RelativeLayout(相對布局)用法知識點(diǎn)以及對應(yīng)的實(shí)例內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-02-02
Android利用GridView實(shí)現(xiàn)單選效果
本篇文章主要介紹了Android利用GridView實(shí)現(xiàn)單選效果的相關(guān)知識,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-05-05
Android手機(jī)獲取root權(quán)限并實(shí)現(xiàn)關(guān)機(jī)重啟功能的方法
這篇文章主要介紹了Android手機(jī)獲取root權(quán)限并實(shí)現(xiàn)關(guān)機(jī)重啟功能的方法,是Android程序設(shè)計(jì)中非常重要的技巧,需要的朋友可以參考下2014-08-08
android 自定義ScrollView實(shí)現(xiàn)背景圖片伸縮的實(shí)現(xiàn)代碼及思路
本文純屬個(gè)人見解,是對前面學(xué)習(xí)的總結(jié),如有描述不正確的地方還請高手指正~,首先還是按照通例給大家看下示例.2013-05-05
Android使用BroadcastReceiver實(shí)現(xiàn)手機(jī)開機(jī)之后顯示畫面的功能
這篇文章主要介紹了Android使用BroadcastReceiver實(shí)現(xiàn)手機(jī)開機(jī)之后顯示畫面的功能,結(jié)合實(shí)例形式分析了BroadcastReceiver的具體使用技巧及實(shí)現(xiàn)開機(jī)畫面的相關(guān)功能代碼,需要的朋友可以參考下2016-01-01
Android中EditText的drawableRight屬性設(shè)置點(diǎn)擊事件
這篇文章主要介紹了Android中EditText的drawableRight屬性的圖片設(shè)置點(diǎn)擊事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Flutter開發(fā)之設(shè)置應(yīng)用名稱及圖標(biāo)的教程
這篇文章主要介紹了Flutter設(shè)置應(yīng)用名稱及圖標(biāo)的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Android使用ViewFlipper實(shí)現(xiàn)圖片上下自動輪播的示例代碼
這篇文章主要介紹了Android使用ViewFlipper實(shí)現(xiàn)圖片上下自動輪播的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05

