Android動(dòng)態(tài)自定義圓形進(jìn)度條
效果圖:

A.繪制圓環(huán),圓弧,文本
//1.畫圓環(huán) //原點(diǎn)坐標(biāo) float circleX = width / 2; float circleY = width / 2; //半徑 float radius = width / 2 - roundWidth / 2; //設(shè)置畫筆的屬性 paint.setColor(roundColor); paint.setStrokeWidth(roundWidth); paint.setStyle(Paint.Style.STROKE); canvas.drawCircle(circleX, circleY, radius, paint); //2.畫圓弧 RectF oval = new RectF(roundWidth/2,roundWidth/2,width-roundWidth/2,width - roundWidth/2); paint.setColor(roundProgressColor); canvas.drawArc(oval, 0, progress * 360 / max, false, paint); //3.畫文本 paint.setTextSize(textSize); paint.setColor(textColor); paint.setStrokeWidth(0); String text = progress * 100 / max + "%"; Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); canvas.drawText(text, width / 2 - bounds.width() / 2, width / 2 + bounds.height() / 2, paint);
B.自定義屬性的具體步驟
具體步驟:
1. 定義屬性: 在values目錄下創(chuàng)建attrs.xml
<declare-styleable name="RoundProgress"> <attr name="roundColor" format="color"></attr> <attr name="roundProgressColor" format="color"></attr> <attr name="textColor" format="color"></attr> <attr name="roundWidth" format="dimension"></attr> <attr name="textSize" format="dimension"></attr> </declare-styleable>
2. 在布局文件中引用當(dāng)前應(yīng)用的名稱空間
xmlns:atguigu=http://schemas.android.com/apk/res-auto
3. 在自定義視圖標(biāo)簽中使用自定義屬性
<com.atguigu.p2p.util.RoundProgress android:id="@+id/rp_home_progress" android:layout_width="120dp" android:layout_height="120dp" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" atguigu:roundColor="@android:color/darker_gray <br> atguigu:roundProgressColor="@android:color/holo_red_dark" atguigu:textColor="@color/text_progress" atguigu:roundWidth="10dp" atguigu:textSize="20sp" />
4. 在自定義View類的構(gòu)造方法中, 取出布局中的自定義屬性值
//1.得到所有自定義屬性的數(shù)組 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgress); //2.獲取自定義屬性的值, 如果沒(méi)有指定取默認(rèn)值 roundColor = typedArray.getColor(R.styleable.RoundProgress_roundColor, Color.RED); roundProgressColor = typedArray.getColor(R.styleable.RoundProgress_roundProgressColor, Color.GREEN); textColor = typedArray.getColor(R.styleable.RoundProgress_textColor, Color.GREEN); roundWidth = typedArray.getDimension(R.styleable.RoundProgress_roundWidth, UIUtils.dp2px(10)); textSize = typedArray.getDimension(R.styleable.RoundProgress_textSize, UIUtils.dp2px(20)); //3.釋放資源數(shù)據(jù) typedArray.recycle();
C.讓圓環(huán)進(jìn)度"動(dòng)起來(lái)"
1.自定義RoundProgress類中提供進(jìn)度屬性的getter和setter方法
2.在HomeFragment的onSuccess()中:
github:https://github.com/ganchuanpu/P2PInvest
以上所述是小編給大家介紹的Android動(dòng)態(tài)自定義圓形進(jìn)度條,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android實(shí)現(xiàn)手機(jī)監(jiān)控?cái)z像頭
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手機(jī)監(jiān)控?cái)z像頭,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Android實(shí)現(xiàn)系統(tǒng)消息推送
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)系統(tǒng)消息推送,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Android 再按一次返回鍵退出程序?qū)崿F(xiàn)思路
用戶退出應(yīng)用前給出一個(gè)提示是很有必要的,因?yàn)榭赡苁怯脩舨⒉徽娴南胪顺觯皇且徊恍⌒陌聪铝朔祷劓I,大部分應(yīng)用的做法是在應(yīng)用退出去前給出一個(gè)Dialog提示框;個(gè)人覺(jué)得再按一次返回鍵退出程序很有必要,接下來(lái)介紹一些簡(jiǎn)單實(shí)現(xiàn)2013-01-01
Android下拉刷新完全解析,教你如何一分鐘實(shí)現(xiàn)下拉刷新功能(附源碼)
以下是我自己花功夫編寫了一種非常簡(jiǎn)單的下拉刷新實(shí)現(xiàn)方案,現(xiàn)在拿出來(lái)和大家分享一下。相信在閱讀完本篇文章之后,大家都可以在自己的項(xiàng)目中一分鐘引入下拉刷新功能2013-07-07
快速解決fragment中onActivityResult不調(diào)用的問(wèn)題
下面小編就為大家?guī)?lái)一篇快速解決fragment中onActivityResult不調(diào)用的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽流效果
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽流效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
Android自定義View實(shí)現(xiàn)標(biāo)簽流效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)標(biāo)簽流效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
如何自己實(shí)現(xiàn)Android View Touch事件分發(fā)流程
這篇文章主要介紹了如何自己實(shí)現(xiàn)Android View Touch事件分發(fā)流程,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03
Flutter開發(fā)setState能否在build中直接調(diào)用詳解
這篇文章主要為大家介紹了Flutter開發(fā)setState能否在build中直接調(diào)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10

