Android應(yīng)用中怎么自定義一個(gè)圓形進(jìn)度條

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Android應(yīng)用中怎么自定義一個(gè)圓形進(jìn)度條,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)整合營銷推廣、網(wǎng)站重做改版、達(dá)日網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁面制作、商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為達(dá)日等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

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.獲取自定義屬性的值, 如果沒有指定取默認(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)度"動起來"

1.自定義RoundProgress類中提供進(jìn)度屬性的getter和setter方法

2.在HomeFragment的onSuccess()中:

上述就是小編為大家分享的Android應(yīng)用中怎么自定義一個(gè)圓形進(jìn)度條了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

文章題目:Android應(yīng)用中怎么自定義一個(gè)圓形進(jìn)度條
文章出自:http://www.muchs.cn/article48/pdhjhp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)外貿(mào)建站、搜索引擎優(yōu)化、品牌網(wǎng)站制作、靜態(tài)網(wǎng)站、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司