網站首頁 編程語言 正文
Android自定義ProgressBar實現漂亮的進度提示框_Android
作者:特立獨行的貓a??于?2021-09-15?12:27:52?發布??1084 ? 更新時間: 2022-08-13 編程語言在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://blog.csdn.net/yyz_1987/article/details/120305518
相關推薦
- 2022-06-13 jupyter?notebook內核啟動失敗問題及解決方法_python
- 2022-04-21 提升Python編碼能力的3個重要概念_python
- 2022-07-06 使用client?go實現自定義控制器的方法_Golang
- 2022-06-25 詳解如何基于Pyecharts繪制常見的直角坐標系圖表_python
- 2022-04-22 element的el-drawer預留操作欄問題
- 2022-05-22 jQuery常用事件方法mouseenter+mouseleave+hover_jquery
- 2022-08-17 jQuery實現購物車_jquery
- 2023-05-29 Python中Merge使用的示例詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支