網(wǎng)站首頁 編程語言 正文
Android自定義ProgressBar實現(xiàn)漂亮的進度提示框_Android
作者:特立獨行的貓a??于?2021-09-15?12:27:52?發(fā)布??1084 ? 更新時間: 2022-08-13 編程語言在android智能平板設(shè)備應(yīng)用中,一項耗時的操作總要有個提示進度的框來提高用戶的操作體驗,操作進度提示框就顯得很常用了。
系統(tǒng)自帶的有進度條ProgressBar,一般用于顯示一個過程,例如數(shù)據(jù)加載過程,文件下載進度,音樂播放進度等。但是樣式太單一不好看,因此有必要自定義一個方便使用。
以下記錄下封裝的進度展示對話框ProgressDialog。
先來展示下效果圖:
需要準備好素材。如上圖中的那個旋轉(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),值為負時逆時針旋轉(zhuǎn) ? ? android:pivotX="50%" ? ? ? ? ? ? ? #旋轉(zhuǎn)中心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" ? ? ? ? ? #動畫結(jié)束后,保持結(jié)束時的狀態(tài) ? ? android:fillBefore="true" ? ? ? ? ?#動畫結(jié)束后,恢復(fù)為初始狀態(tài) ? ? android:fillEnabled="true" ? ? ? ? #效果同上 ? ? android:repeatCount="5" ? ? ? ? ? ?#重復(fù)次數(shù),取值為-1時無限重復(fù),默認動畫執(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布局,顯示出來即可。
在需要提示進度的地方,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()
? ? ? ? ? ? }
原文鏈接:https://blog.csdn.net/yyz_1987/article/details/120305518
相關(guān)推薦
- 2022-07-21 C#?PDF轉(zhuǎn)圖片(JPG,Png)的項目實踐_C#教程
- 2024-03-06 SpringBoot 項目 批量刪除的操作
- 2022-03-06 正則表達式用法詳解_正則表達式
- 2022-09-05 C語言之把數(shù)組名作函數(shù)參數(shù)的四種情況說明_C 語言
- 2022-08-03 GoFrame?gmap遍歷hashmap?listmap?treemap使用技巧_Golang
- 2022-06-12 查看docker中運行的JVM參數(shù)問題及解決方法_docker
- 2022-05-04 詳解Tomcat中查看JVM內(nèi)存使用情況_Tomcat
- 2023-07-08 vscode上查看git的記錄,可以看到是誰多久前修改的代碼
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支