Android自定義ProgressBar實現(xiàn)漂亮的進度提示框
在android智能平板設備應用中,一項耗時的操作總要有個提示進度的框來提高用戶的操作體驗,操作進度提示框就顯得很常用了。
系統(tǒng)自帶的有進度條ProgressBar,一般用于顯示一個過程,例如數(shù)據(jù)加載過程,文件下載進度,音樂播放進度等。但是樣式太單一不好看,因此有必要自定義一個方便使用。
以下記錄下封裝的進度展示對話框ProgressDialog。
先來展示下效果圖:
需要準備好素材。如上圖中的那個旋轉的圈圈,素材圖是一張png圖片,分辨率114x114:
如何實現(xiàn)自動旋轉的效果呢,使用android的Rotate動畫。
在res/drawable下建一個rotate_dialog_progress.xml文件,內容如下:
<?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" ? ? ? ? ? #結束時角度,值為正時順時針旋轉,值為負時逆時針旋轉 ? ? android:pivotX="50%" ? ? ? ? ? ? ? #旋轉中心x軸坐標,取值可以是數(shù)值(50)、百分數(shù)(50%)、百 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 分數(shù)p(50%p),當取值為數(shù)值時,縮放起點為View左上角坐標 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 加具體數(shù)值像素,當取值為百分數(shù)時,表示在當前View左上角坐 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 加上View寬度的具體百分比,當取值為百分數(shù)p時,表示在View ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 左上角坐標加上父控件寬度的具體百分比 ? ? android:pivotY="50%" ? ? ? ? ? ? ? #同上 ? ? android:duration="700" ? ? ? ? ? ? #動畫持續(xù)時間,毫秒為單位 ? ? android:fillAfter="true" ? ? ? ? ? #動畫結束后,保持結束時的狀態(tài) ? ? android:fillBefore="true" ? ? ? ? ?#動畫結束后,恢復為初始狀態(tài) ? ? android:fillEnabled="true" ? ? ? ? #效果同上 ? ? android:repeatCount="5" ? ? ? ? ? ?#重復次數(shù),取值為-1時無限重復,默認動畫執(zhí)行一次 ? ? android:repeatMode ="reverse" ? ? ?#重復模式,有reverse和restart兩個值,前者為倒序回放,后者為重新開始 ? ? android:interpolator="@android:anim/accelerate_decelerate_interpolator" #插值器 ? ? ? ? ? ? ? ? />
接下來在styles.xml文件中定義一個樣式文件供使用。內容如下:
<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"
它就是一個放在res/drawable文件夾下的一個自定義shape。
corner_bg_dialog_progress.xml文件內容如下:
<?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布局,顯示出來即可。
在需要提示進度的地方,showProgressDialog。在結束時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() ? ? ? ? ? ? }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android Activity之間相互調用與傳遞參數(shù)的原理與用法分析
這篇文章主要介紹了Android Activity之間相互調用與傳遞參數(shù)的原理與用法,較為詳細的分析了Android組件的構成以及Activity的創(chuàng)建、調用、切換等相關操作技巧,需要的朋友可以參考下2016-08-08Android Studio升級到3.0 Terminal 中文顯示異常解決
本篇文章主要介紹了Android Studio升級到3.0 Terminal 中文顯示異常解決,非常具有實用價值,需要的朋友可以參考下2017-10-10Android webview和js互相調用實現(xiàn)方法
這篇文章主要介紹了 Android webview和js互相調用實現(xiàn)方法的相關資料,需要的朋友可以參考下2016-10-10Android監(jiān)聽手機電話狀態(tài)與發(fā)送郵件通知來電號碼的方法(基于PhoneStateListene實現(xiàn))
這篇文章主要介紹了Android監(jiān)聽手機電話狀態(tài)與發(fā)送郵件通知來電號碼的方法,通過Android的PhoneStateListene實現(xiàn)該功能,需要的朋友可以參考下2016-01-01