在android智能平板設備應用中,一項耗時的操作總要有個提示進度的框來提高用戶的操作體驗,操作進度提示框就顯得很常用了,
系統自帶的有進度條ProgressBar,一般用于顯示一個程序,例如資料加載程序,檔案下載進度,音樂播放進度等,但是樣式太單一不好看,因此有必要自定義一個方便使用,
以下記錄下封裝的進度展示對話框ProgressDialog,
先來展示下效果圖:

需要準備好素材,如上圖中的那個旋轉的圈圈,素材圖是一張png圖片,解析度114x114:

如何實作自動旋轉的效果呢,使用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軸坐標,取值可以是數值(50)、百分數(50%)、百
分數p(50%p),當取值為數值時,縮放起點為View左上角坐標
加具體數值像素,當取值為百分數時,表示在當前View左上角坐
加上View寬度的具體百分比,當取值為百分數p時,表示在View
左上角坐標加上父控制元件寬度的具體百分比
android:pivotY="50%" #同上
android:duration="700" #影片持續時間,毫秒為單位
android:fillAfter="true" #影片結束后,保持結束時的狀態
android:fillBefore="true" #影片結束后,恢復為初始狀態
android:fillEnabled="true" #效果同上
android:repeatCount="5" #重復次數,取值為-1時無限重復,默認影片執行一次
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對話框中來用,看下對框框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檔案內容如下:
<?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>
最后,實作一個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()
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/300775.html
標籤:其他
