使用
<RoundProgressBar
app:radius="17dp" //角度
app:line_width="4dp" //线条粗细
app:color="#f3b024" //上面颜色
app:bg_color="#0085e3" //底背景颜色
/>
- 在 attrs文件添加
<declare-styleable name="RoundProgressBar">
<attr name="color" format="color"></attr>
<attr name="bg_color" format="color"></attr>
<attr name="line_width" format="dimension"></attr>
<attr name="radius" format="dimension"></attr>
<attr name="android:progress" ></attr>
</declare-styleable>`
2.java文件
public class RoundProgressBar extends View {
private Paint mBackPaint;
private Paint mFrontPaint;
private float mLineWidth;
private int mRadius;
private RectF mRect;
private int mProgress;
private int mMax = 100;
private int mColor;
private int mBgColor;
public RoundProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);
mRadius = (int) ta.getDimension(R.styleable.RoundProgressBar_radius, dp2px(30));
mColor = ta.getColor(R.styleable.RoundProgressBar_color, 0xffff0000);
mBgColor = ta.getColor(R.styleable.RoundProgressBar_bg_color, 0xffff0000);
mLineWidth = (int) ta.getDimension(R.styleable.RoundProgressBar_line_width, dp2px(3));
mProgress = ta.getInt(R.styleable.RoundProgressBar_android_progress, 30);
ta.recycle();
init();
}
private float dp2px(int dpVal)
{
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, dpVal, getResources().getDisplayMetrics());
}
//完成相关参数初始化
private void init() {
mBackPaint = new Paint();
mBackPaint.setColor(mColor);
mBackPaint.setAntiAlias(true);
mBackPaint.setStyle(Paint.Style.STROKE);
mBackPaint.setStrokeWidth(mLineWidth);
mFrontPaint = new Paint();
mFrontPaint.setColor(mBgColor);
mFrontPaint.setAntiAlias(true);
mFrontPaint.setStyle(Paint.Style.STROKE);
mFrontPaint.setStrokeWidth(mLineWidth);
}
//标准写法
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int width = 0;
if (widthMode == MeasureSpec.EXACTLY)
{
width = widthSize;
} else
{
int needWidth = measureWidth() + getPaddingLeft() + getPaddingRight();
if (widthMode == MeasureSpec.AT_MOST)
{
width = Math.min(needWidth, widthSize);
} else
{
width = needWidth;
}
}
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int height = 0;
if (heightMode == MeasureSpec.EXACTLY)
{
height = heightSize;
} else
{
int needHeight = measureHeight() + getPaddingTop() + getPaddingBottom();
if (heightMode == MeasureSpec.AT_MOST)
{
height = Math.min(needHeight, heightSize);
} else //MeasureSpec.UNSPECIFIED
{
height = needHeight;
}
}
setMeasuredDimension(width, height);
}
private int measureHeight()
{
return mRadius * 2;
}
private int measureWidth()
{
return mRadius * 2;
}
@Override
protected void onDraw(Canvas canvas) {
initRect();
int width = getWidth();
int height = getHeight();
float angle = mProgress / (float) mMax * -360;
canvas.drawCircle(width / 2, height / 2, mRadius, mBackPaint);
canvas.drawArc(mRect, -90, angle, false, mFrontPaint);
}
private void initRect() {
int width = getWidth();
int height = getHeight();
if (mRect == null) {
mRect = new RectF();
int viewSize = (int) (mRadius * 2);
int left = (width - viewSize) / 2;
int top = (height - viewSize) / 2;
int right = left + viewSize;
int bottom = top + viewSize;
mRect.set(left, top, right, bottom);
}
}
public void setProgress(int mProgress){
this.mProgress = mProgress;
invalidate();
}
public int getProgress() {
return mProgress;
}
}