自定義view之圓環(huán)進(jìn)度條

注解已經(jīng)很詳細(xì)了,請(qǐng)多多指教

/**

* 1.自定義view

* 2.@authorDell

* 3.@date2017/10/9 09:08

*/

public class XCRoundProgressBar extends View{

private Paintpaint;//畫(huà)筆對(duì)象的引用

private int roundColor;//圓環(huán)的顏色

private int roundProgressColor;//圓環(huán)進(jìn)度的顏色

private int innerRoundColor;//圓環(huán)內(nèi)部圓顏色

private float roundWidth;//圓環(huán)的寬度

private int textColor;//中間進(jìn)度百分比字符串的顏色

private float textSize;//中間進(jìn)度百分比字符串的字體

private int max;//最大進(jìn)度

private int progress;//當(dāng)前進(jìn)度

private boolean isDisplayText;//是否顯示中間百分比進(jìn)度字符串

private intstyle;//進(jìn)度條的風(fēng)格:空心圓環(huán)或者實(shí)心圓環(huán)

private static final intSTROKE=0;//空心

private static final intFILL=1;//實(shí)心

public XCRoundProgressBar(Context context){

this(context,null);

}

public XCRoundProgressBar(Context context, AttributeSet attrs){

this(context,attrs,0);

}

public XCRoundProgressBar(Context context, AttributeSet attrs,

int defStyleAttr) {

super(context, attrs, defStyleAttr);

//TODO Auto-generated constructor stub

paint=newPaint();

//從attrs.xml中獲取自定義屬性和默認(rèn)值

TypedArray typedArray? = context.obtainStyledAttributes(attrs, R.styleable.XCRoundProgressBar);

roundColor= typedArray.getColor(R.styleable.XCRoundProgressBar_roundColor, Color.GREEN);

roundProgressColor= typedArray.getColor(R.styleable.XCRoundProgressBar_roundProgressColor, Color.RED);

innerRoundColor= typedArray.getColor(R.styleable.XCRoundProgressBar_innerRoundColor, Color.GRAY);

roundWidth= typedArray.getDimension(R.styleable.XCRoundProgressBar_roundWidth,5);

textColor=typedArray.getColor(R.styleable.XCRoundProgressBar_textColor, Color.RED);

textSize= typedArray.getDimension(R.styleable.XCRoundProgressBar_textSize,15);

max= typedArray.getInteger(R.styleable.XCRoundProgressBar_max,100);

style= typedArray.getInt(R.styleable.XCRoundProgressBar_style,STROKE);

isDisplayText=typedArray.getBoolean(R.styleable.XCRoundProgressBar_textIsDisplayable,true);

typedArray.recycle();

}

@Override

protected void onDraw(Canvas canvas) {

//TODO Auto-generated method stub

super.onDraw(canvas);

//畫(huà)最外層大圓環(huán)

int centerX = getWidth()/2;//獲取中心點(diǎn)X坐標(biāo)

int certerY = getHeight()/2;//獲取中心點(diǎn)Y坐標(biāo)

int radius =(int)(centerX -roundWidth/2);//圓環(huán)的半徑

paint.setColor(roundColor);

paint.setStyle(Paint.Style.STROKE);//設(shè)置空心

paint.setStrokeWidth(roundWidth);//設(shè)置圓環(huán)寬度

paint.setAntiAlias(true);//消除鋸齒

canvas.drawCircle(centerX,certerY, radius,paint);//繪制圓環(huán)

//繪制圓環(huán)內(nèi)部圓

paint.setColor(innerRoundColor);

paint.setStyle(Paint.Style.FILL);

paint.setAntiAlias(true);

canvas.drawCircle(centerX, certerY, radius-roundWidth/2,paint);

//畫(huà)進(jìn)度

paint.setStrokeWidth(roundWidth);//設(shè)置圓環(huán)寬度

paint.setColor(roundProgressColor);//設(shè)置進(jìn)度顏色

RectF oval =newRectF(centerX - radius, centerX - radius, centerX

+ radius, centerX + radius);//用于定義的圓弧的形狀和大小的界限

switch(style) {

case STROKE: {

paint.setStyle(Paint.Style.STROKE);

canvas.drawArc(oval,0,360*progress/max,false,paint);// 根據(jù)進(jìn)度畫(huà)圓弧

break;

}

case FILL: {

paint.setStyle(Paint.Style.FILL);

if(progress!=0)

canvas.drawArc(oval,0,360*progress/max,true,paint);// 根據(jù)進(jìn)度畫(huà)圓弧

break;

}

}

//畫(huà)中間進(jìn)度百分比字符串

paint.setStrokeWidth(0);

paint.setColor(textColor);

paint.setTextSize(textSize);

paint.setTypeface(Typeface.DEFAULT_BOLD);

int percent = (int)(((float)progress/ (float)max) *100);//計(jì)算百分比

float textWidth =paint.measureText(percent +"%");//測(cè)量字體寬度,需要居中顯示

if(isDisplayText&&style==STROKE&& percent !=0){

canvas.drawText(percent+"%", centerX-textWidth/2, centerX +textSize/2,paint);

}

}

public Paint getPaint() {

return paint;

}

public void setPaint(Paint paint) {

this.paint= paint;

}

public int getRoundColor() {

return roundColor;

}

public void setRoundColor(int roundColor) {

this.roundColor= roundColor;

}

public int getRoundProgressColor() {

return roundProgressColor;

}

public void setRoundProgressColor(int roundProgressColor) {

this.roundProgressColor= roundProgressColor;

}

public float getRoundWidth() {

return roundWidth;

}

public void setRoundWidth(float roundWidth) {

this.roundWidth= roundWidth;

}

public int getTextColor() {

return textColor;

}

public void setTextColor(int textColor) {

this.textColor= textColor;

}

public float getTextSize() {

return textSize;

}

public void setTextSize(float textSize) {

this.textSize= textSize;

}

public synchronized int getMax() {

return max;

}

public synchronized void setMax(intmax) {

if(max <0){

throw new IllegalArgumentException("max must more than 0");

}

this.max= max;

}

public synchronized int getProgress() {

return progress;

}

/**

* 設(shè)置進(jìn)度,此為線(xiàn)程安全控件,由于考慮多線(xiàn)的問(wèn)題,需要同步

* 刷新界面調(diào)用postInvalidate()能在非UI線(xiàn)程刷新

*@authorcaizhiming

*/

public synchronized void setProgress(int progress) {

if(progress <0){

throw new IllegalArgumentException("progress must more than 0");

}

if(progress >max){

this.progress= progress;

}

if(progress <=max){

this.progress= progress;

postInvalidate();

}

}

public boolean isDisplayText() {

return isDisplayText;

}

public void setDisplayText(boolean isDisplayText) {

this.isDisplayText= isDisplayText;

}

public int getStyle() {

return style;

}

public void setStyle(int style) {

this.style= style;

}

}

//布局文件

<com.bhx.bihongxin20171009.view.XCRoundProgressBar

android:id="@+id/bar"

android:layout_width="80dp"

android:layout_height="80dp"

app:roundColor="#CCC"

app:roundProgressColor="#000"

app:roundWidth="7dp"

app:innerRoundColor="#fff"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true"

/>

//屬性文件

<?xml version="1.0"encoding="utf-8"?>

<resources>

<declare-styleablename="XCRoundProgressBar">

<attr name="roundColor" format="color"/>

<attr name="roundProgressColor" format="color"/>

<attr name="roundWidth" format="dimension"/>

<attr name="innerRoundColor" format="color"/>

<attr name="textColor" format="color"/>

<attr name="textSize" format="dimension"/>

<attr name="max" format="integer"/>

<attr name="textIsDisplayable" format="boolean"/>

<attr name="style">

<enum name="STROKE" ?value="0"></enum>

<enum name="FILL" value="1"></enum>

</attr>

</declare-styleable>

</resources>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容