一、自定义属性
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CircleProgressView"> <attr name="text" format="string"/> <attr name="textColor" format="color|reference"/> <attr name="textSzie" format="reference|dimension"/> <attr name="bgCircleColor" format="color|reference"/> <attr name="bgCircleStrokeWidth" format="dimension|reference"/> <attr name="progressCircleColor" format="color|reference"/> <attr name="progressStrokeWidth" format="dimension|reference"/> <attr name="maxProgress" format="integer"/> <attr name="progress" format="integer"/> </declare-styleable> </resources>
二、java代码实现 public class CircleProgressView extends View{ private String mText; private int mTextColor; private float mTextSize; private int mBackgroudCircleColor; private float mBackgroundCircleStrokeWidth; private int mProgressCircleColor; private float mProgressCircleWidth; private int mMaxProgress; private int mProgess; private Paint mPaint; public CircleProgressView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressView); mText = typedArray.getString(R.styleable.CircleProgressView_text); mTextColor = typedArray.getColor(R.styleable.CircleProgressView_textColor,getResources().getColor(R.color. colorAccent)); mTextSize = typedArray.getDimension(R.styleable.CircleProgressView_textSzie, getResources().getDimension( R.dimen.circleProgressTextSize)); mBackgroudCircleColor = typedArray.getColor(R.styleable.CircleProgressView_bgCircleColor,getResources().getColor(R. color.greenOne)); mBackgroundCircleStrokeWidth = typedArray.getDimension(R.styleable.CircleProgressView_bgCircleStrokeWidth,getResources(). getDimension(R.dimen.bgCircleProgressStrokeWidth)); mProgressCircleColor = typedArray.getColor(R.styleable.CircleProgressView_progressCircleColor,getResources(). getColor(R.color.redOne)); mProgressCircleWidth = typedArray.getDimension(R.styleable.CircleProgressView_progressStrokeWidth,getResources(). getDimension(R.dimen.bgCircleProgressStrokeWidth)); mMaxProgress = typedArray.getInteger(R.styleable.CircleProgressView_maxProgress,100); mProgess = typedArray.getInteger(R.styleable.CircleProgressView_progress,50); typedArray.recycle(); mPaint = new Paint(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int diameter = getWidth() > getHeight()? getHeight() :getWidth(); float radius = diameter/2.0f; mPaint.setColor(mBackgroudCircleColor); mPaint.setStrokeWidth(mBackgroundCircleStrokeWidth); mPaint.setStyle(Paint.Style.STROKE); canvas.drawCircle(radius,radius,radius - mBackgroundCircleStrokeWidth/2,mPaint); if(mProgess > mMaxProgress || mProgess < 0){ mProgess = mMaxProgress; } mPaint.setColor(mProgressCircleColor); mPaint.setStrokeWidth(mProgressCircleWidth); RectF rectF = new RectF(0 + mBackgroundCircleStrokeWidth/2,0+ mBackgroundCircleStrokeWidth/2, diameter - mBackgroundCircleStrokeWidth/2,diameter - mBackgroundCircleStrokeWidth/2); canvas.drawArc(rectF,-90,360.0f*mProgess/mMaxProgress,false,mPaint); if(!TextUtils.isEmpty(mText)){ mPaint.setTextSize(mTextSize); mPaint.setColor(mTextColor); mPaint.setStyle(Paint.Style.FILL); float length = mPaint.measureText(mText); canvas.drawText(mText,radius - length/2,radius + mTextSize/2,mPaint); } } public void updateProgress(int progress){ if(mProgess != progress){ if(0 <= progress && progress <= 100){ mProgess = progress; mText = mProgess + "%"; postInvalidate(); } } } }