一.效果图
public class CircleProgressView extends View {
//外圈颜色
private int mOuterColor = Color.GRAY;
//内圈颜色
private int mInterColor = Color.BLUE;
//弧宽度
private int mBorderWidth = 10; //10px
private int mStepTextSize;
private int mStepTextColor;
private Paint mOuterPaint;
private Paint mInnerPaint;
private int mMaxStep = 0;
private int mCurrentStep = 0;
public CircleProgressView(Context context) {
this(context, null);
}
public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CircleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取xml里的属性值
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressView);
mOuterColor = array.getColor(R.styleable.CircleProgressView_outerColor, mOuterColor);
mInterColor = array.getColor(R.styleable.CircleProgressView_innerColor, mInterColor);
mBorderWidth = (int) array.getDimension(R.styleable.CircleProgressView_borderWidth, mBorderWidth);
//释放资源
array.recycle();
//初始化外部画笔
mOuterPaint = new Paint();
mOuterPaint.setAntiAlias(true);
mOuterPaint.setColor(mOuterColor);
mOuterPaint.setStrokeWidth(mBorderWidth);//width 是弧的内外扩散的
mOuterPaint.setStyle(Paint.Style.STROKE);//Fill画笔实心,Stroke描边
mOuterPaint.setStrokeCap(Paint.Cap.ROUND);
//初始化内部画笔
mInnerPaint = new Paint();
mInnerPaint.setAntiAlias(true);
mInnerPaint.setColor(mInterColor);
mInnerPaint.setStrokeWidth(mBorderWidth);//width 是弧的内外扩散的
mInnerPaint.setStyle(Paint.Style.STROKE);//Fill画笔实心,Stroke描边
mInnerPaint.setStrokeCap(Paint.Cap.ROUND);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//宽高不一致,取小的,保证是个正方形
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width > height ? height : width, width > height ? height : width);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int center = getWidth() / 2;
int radius = getWidth() / 2 - mBorderWidth / 2;
RectF rectF = new RectF(center-radius, center-radius, center+radius, center+radius);
//绘制外圆弧
canvas.drawArc(rectF, 135, 270, false, mOuterPaint);
if (mMaxStep == 0) {
return;
}
float sweepAngle = (float) mCurrentStep / mMaxStep;
//绘制内圆弧
canvas.drawArc(rectF, 135, sweepAngle * 270, false, mInnerPaint);
}
public synchronized void setStepMax(int mMaxStep) {
this.mMaxStep = mMaxStep;
}
public synchronized void setCurrentStep(int mCurrentStep) {
this.mCurrentStep = mCurrentStep;
invalidate();
}
}
<com.text.ui.widget.CircleProgressView
android:id="@+id/circle_progress"
android:layout_width="@dimen/px_331"
android:layout_height="@dimen/px_331"
android:layout_marginTop="@dimen/px41"
android:layout_marginStart="@dimen/px_102"
app:outerColor= "@color/btn_weak_color"
app:innerColor="@color/btn_color_1"/>