先上效果图吧
上面的进度条,开始做的时候找了几个不太一样.
然后就自己做了一个修改,继承原生View自己画的
初始化圆的半径和圆心点坐标
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
int min = Math.min(width - getPaddingLeft() - getPaddingRight(), height - getPaddingTop() - getPaddingBottom());
setMeasuredDimension(width, height);
mColorWheelRadius = (min - pressExtraStrokeWidth) / 2;//计算环形进度的半径
centerX = (width - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();//计算圆心点的坐标
centerY = (height - getPaddingTop() - getPaddingBottom()) / 2 + getPaddingTop();
mColorWheelRectangle.set(centerX - mColorWheelRadius, centerY - mColorWheelRadius, centerX + mColorWheelRadius, centerY + mColorWheelRadius);
}
初始化画笔
private void init() {
//默认圆弧
mNormalRingPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mNormalRingPaint.setColor(Color.parseColor("#EFEFEF"));
mNormalRingPaint.setStrokeWidth(pressExtraStrokeWidth);
mNormalRingPaint.setStyle(Paint.Style.STROKE);
//开启显示边缘为圆形
mNormalRingPaint.setStrokeCap(Paint.Cap.ROUND);
//进度圆环
mProgressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mProgressPaint.setStrokeCap(Paint.Cap.ROUND);
mProgressPaint.setStyle(Paint.Style.STROKE);
mProgressPaint.setColor(Color.RED);
mProgressPaint.setStrokeWidth(pressExtraStrokeWidth);
}
最后就是我们的ondraw方法了
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawArc(mColorWheelRectangle, 120, 300, false, mNormalRingPaint);
//颜色渐变
SweepGradient sweepGradient = new SweepGradient(centerX, centerY, new int[]{Color.parseColor("#3BA6FC"), Color.parseColor("#6eddff")}, new
float[]{0f, 0.83f});
mProgressPaint.setShader(sweepGradient);
//画出来后旋转画布
canvas.rotate(118, centerX, centerY);
canvas.drawArc(mColorWheelRectangle, 2, getCurrentprogress(), false, mProgressPaint);
}