自定義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;//畫筆對(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á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);

//畫進(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)度畫圓弧

break;

}

case FILL: {

paint.setStyle(Paint.Style.FILL);

if(progress!=0)

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

break;

}

}

//畫中間進(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)度,此為線程安全控件梅惯,由于考慮多線的問題,需要同步

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

*@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)系作者
  • 序言:七十年代末捞烟,一起剝皮案震驚了整個(gè)濱河市吠卷,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌岛心,老刑警劉巖来破,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異忘古,居然都是意外死亡徘禁,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門髓堪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來送朱,“玉大人娘荡,你說我怎么就攤上這事∈徽樱” “怎么了炮沐?”我有些...
    開封第一講書人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長回怜。 經(jīng)常有香客問我大年,道長,這世上最難降的妖魔是什么玉雾? 我笑而不...
    開封第一講書人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任翔试,我火速辦了婚禮,結(jié)果婚禮上复旬,老公的妹妹穿的比我還像新娘垦缅。我一直安慰自己,他們只是感情好赢底,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開白布失都。 她就那樣靜靜地躺著,像睡著了一般幸冻。 火紅的嫁衣襯著肌膚如雪粹庞。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評(píng)論 1 301
  • 那天洽损,我揣著相機(jī)與錄音庞溜,去河邊找鬼。 笑死碑定,一個(gè)胖子當(dāng)著我的面吹牛流码,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播延刘,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼漫试,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了碘赖?” 一聲冷哼從身側(cè)響起驾荣,我...
    開封第一講書人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎普泡,沒想到半個(gè)月后播掷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡撼班,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年歧匈,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片砰嘁。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡件炉,死狀恐怖勘究,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情妻率,我是刑警寧澤乱顾,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站宫静,受9級(jí)特大地震影響走净,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜孤里,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一伏伯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧捌袜,春花似錦说搅、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至霍衫,卻和暖如春候引,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背敦跌。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來泰國打工澄干, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人柠傍。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓麸俘,卻偏偏與公主長得像,于是被迫代替她去往敵國和親惧笛。 傳聞我的和親對(duì)象是個(gè)殘疾皇子从媚,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容