http://www.reibang.com/p/4e0eb9bb09ab
packagecom.example.mrwang.paintcircu;
importandroid.content.Context;
importandroid.content.res.TypedArray;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.RectF;
importandroid.graphics.Typeface;
importandroid.util.AttributeSet;
importandroid.view.View;
/**
* Created by MrWang on 2017/5/15.
*/
public classRoundProgressBarextendsView {
intmMaxStep;//最大數(shù)值
intprogress;//當(dāng)前進(jìn)度值
intmRoundProgressColor;//半弧進(jìn)度值
intmTextColor;//字體色值
intmRoundColor;//半弧底色
intmRadius;//半徑
intcenterX;//x軸
intcenterY;//y軸
intmPercent;//百分比
floatmRoundWidth;//弧形寬度
PaintmPaint;
publicRoundProgressBar(Context context) {
this(context, null);
}
publicRoundProgressBar(Context context,AttributeSet attrs) {
this(context,attrs,0);
}
publicRoundProgressBar(Context context,AttributeSet attrs, intdefStyleAttr) {
super(context,attrs,defStyleAttr);
mPaint=newPaint();
TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
R.styleable.RoundProgressBar);
//獲取自定義屬性和默認(rèn)值
mRoundColor= mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor,Color.WHITE);
mRoundProgressColor= mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor,Color.parseColor("#fd9426"));
mTextColor= mTypedArray.getColor(R.styleable.RoundProgressBar_textColors,Color.GREEN);
mRoundWidth= mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth,5);
mMaxStep= mTypedArray.getInteger(R.styleable.RoundProgressBar_max,100);
mTypedArray.recycle();
}
@Override
protected voidonDraw(Canvas canvas) {
super.onDraw(canvas);
/**畫背景圓弧*/
//int centerX = mViewWidth / 2;
//int centerY = mViewHeight / 2;
centerX= getWidth() /2;
centerY= getHeight() /2;
//設(shè)置圓弧畫筆的寬度
mPaint.setStrokeWidth(mRoundWidth);
//設(shè)置為ROUND
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeJoin(Paint.Join.ROUND);
//設(shè)置畫筆顏色
mPaint.setColor(mRoundColor);
mPaint.setStyle(Paint.Style.STROKE);
//半徑
mRadius= (int) (centerX-mRoundWidth);
RectF oval =newRectF(centerX-mRadius,centerY-mRadius,centerX+mRadius,centerY+mRadius);
//畫背景圓弧
canvas.drawArc(oval,-180,180, true,mPaint);
/**畫進(jìn)度圓弧*/
mPaint.setColor(mRoundProgressColor);
//計(jì)算當(dāng)前百分比
//float percent = (float) progress / mMaxStep;
//根據(jù)當(dāng)前百分比計(jì)算圓弧掃描的角度
canvas.drawArc(oval,-180,180*progress/mMaxStep, true,mPaint);
/**畫數(shù)字進(jìn)度值* */
mPaint.setStrokeWidth(0);
mPaint.setColor(mTextColor);
mPaint.setTextSize(20);
mPaint.setTypeface(Typeface.DEFAULT_BOLD);//設(shè)置字體
//中間的進(jìn)度百分比墩崩,先轉(zhuǎn)換成float在進(jìn)行除法運(yùn)算邮利,不然都為0
mPercent= (int) (((float)progress/ (float)mMaxStep) *100);
//畫進(jìn)度值顯示位置? 這里也可將"progress"直接當(dāng)進(jìn)度值
canvas.drawText(mPercent+"",getWidth() /2,getHeight() /2,mPaint);
}
//設(shè)置當(dāng)前最大步數(shù)
public synchronized void setMaxStep(intmaxStep) {
if(maxStep <0) {
throw newIllegalArgumentException("maxStep不能小于0!");
}
this.mMaxStep= maxStep;
}
//設(shè)置進(jìn)度
public synchronized void setProgress(intprogress) {
if(progress <0) {
throw newIllegalArgumentException("progress不能小于0!");
}
if(progress >mMaxStep) {
progress =mMaxStep;
}
if(progress <=mMaxStep) {
this.progress= progress;
//重新刷新繪制-> onDraw()
postInvalidate();
}
}
public synchronized intgetProgress() {
return progress;
}
public synchronized intgetMaxStep() {
return mMaxStep;
}
}