版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq998701/article/details/81483134
1.style文件
<attr name="originColor" format="color"/>
<attr name="setColor" format="color"/>
<attr name="textViewColor" format="color"/>
<attr name="TextViewSize" format="dimension"/>
<attr name="circleWidth" format="dimension"/>
<declare-styleable name="CircleProgress">
<attr name="originColor"/>
<attr name="setColor"/>
<attr name="textViewColor"/>
<attr name="TextViewSize"/>
<attr name="circleWidth"/>
</declare-styleable>
2.自定义circleProgress
public class CircleProgress extends View {
private Paint mPaint;
private int mRadius; // 圆环半径
private TextPaint mTextPaint;
private int strokeWidth = 20; //圆环圈的宽度
private int mProgress;
private int mPercent;
private int mOriginColor;
private int mSetColor;
private int mTextViewColor;
private int mTextViewSize;
private int mCount = 0;
public CircleProgress(Context context) {
this(context, null);
}
public CircleProgress(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CircleProgress(Context context, final AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CircleProgress, defStyleAttr, 0);
int indexCount = a.getIndexCount();
for (int i = 0; i < indexCount; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.CircleProgress_circleWidth:
strokeWidth = (int) a.getDimension(attr, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics()));
break;
case R.styleable.CircleProgress_originColor:
mOriginColor = a.getColor(attr, Color.GRAY);
break;
case R.styleable.CircleProgress_setColor:
mSetColor = a.getColor(attr, Color.RED);
break;
case R.styleable.CircleProgress_textViewColor:
mTextViewColor = a.getColor(attr, Color.BLACK);
break;
case R.styleable.CircleProgress_TextViewSize:
mTextViewSize = (int) a.getDimension(attr, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics()));
break;
}
}
a.recycle();
init();
start();
}
private void start() {
new Thread(new Runnable() {
@Override
public void run() {
while (mCount != 361) {
startDraw(mCount);
mCount++;
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
private void init() {
//画数字
mTextPaint = new TextPaint();
mTextPaint.setColor(mTextViewColor);
mTextPaint.setTextSize(mTextViewSize);
mTextPaint.setAntiAlias(true);
//画图
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(strokeWidth);
}
public void startDraw(int progress) {
mProgress = progress;
mPercent = (int) (mProgress / 3.6);
postInvalidate();
}
@Override
protected void onDraw(Canvas canvas) {
int width = getWidth() / 2;
mRadius = width - strokeWidth / 2;
RectF rectF = new RectF(width - mRadius, width - mRadius, width + mRadius, width + mRadius);
mPaint.setColor(mOriginColor);
canvas.drawCircle(width, width, mRadius, mPaint);
mPaint.setColor(mSetColor);
canvas.drawText(mPercent + "%", width - strokeWidth * 2, width + strokeWidth, mTextPaint);
canvas.drawArc(rectF, -90, mProgress, false, mPaint);
}
}