注解已經(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>