先看下效果图:
概述:
1、首先自定义View的属性,画出圆环形状
自定义圆形音量条的流程:
private int mRoundColor = Color.parseColor("#ccffffff");//20%白色透明,圆环背景色
private int mRroundProgressColor = Color.parseColor("#00b4ff");//蓝色,圆环进度颜色
private float mRoundWidth = 25;//圆环宽度
private int mMaxValue = 100;//最大值
private int mCurrentPorgress;
private int startAngle;
public RoundProgressBar(Context context) {
this(context, null);
}
public RoundProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mPaint = new Paint();
startAngle = -90;
setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
2.画出最外圈圆环
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(mRoundWidth);
mPaint.setAntiAlias(true);
canvas.drawCircle(center, center, radius, mPaint);
3.画出最里面圆环
mPaint.setColor(mRroundProgressColor);
RectF oval = new RectF(center - radius, center - radius, center
+ radius, center + radius);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawArc(oval, startAngle, 360 * mCurrentPorgress / mMaxValue,
false, mPaint);
4.设置圆环的进度显示
public synchronized void setProgress(int progress) {
if (progress < 0) {
return;
}
if (progress > mMaxValue) {
progress = mMaxValue;
}
if (progress <= mMaxValue) {
mCurrentPorgress = progress;
postInvalidate();
}
}
至此,自定义圆环进度条就完成,代码很简单,最主要的是理解自定义绘制的原理。