圆形的进度条
今天又写了一个demo,带有两个进度值的 圆环进度条,原谅我是个小白,只能写一写简单的自定义控件,我会继续努力的。。先看效果图
@Override
protected void onDraw(Canvas canvas) {
/*偏移量*/
float dMax = (dotDiameter - maxProgressWidth)*0.5F;
float dFirst = (dotDiameter - firstProgressWidth)*0.5F;
float dSecond = (dotDiameter - secondProgressWidth)*0.5F;
/*1.圆心(x,y)坐标值*/
float centerX = (width - getPaddingLeft() - getPaddingRight()) / 2.0f;
float centerY = (height - getPaddingTop() - getPaddingBottom()) / 2.0f;
/*2.圆环半径*/
float maxRadius = centerY - maxProgressWidth / 2;
float firstRadius = centerY - firstProgressWidth / 2;
float secondRadius = centerY - secondProgressWidth / 2;
if (getWidth() >= getHeight()) {
maxRadius = centerY - maxProgressWidth / 2;
firstRadius = centerY - firstProgressWidth / 2;
secondRadius = centerY - secondProgressWidth / 2;
} else {
maxRadius = centerX - maxProgressWidth / 2;
firstRadius = centerX - firstProgressWidth / 2;
secondRadius = centerX - secondProgressWidth / 2;
}
maxRectF.left = centerX - maxRadius + dMax;
maxRectF.right = centerX + maxRadius - dMax;
maxRectF.top = centerY - maxRadius + dMax;
maxRectF.bottom = centerY + maxRadius - dMax;
firstRectF.left = centerX - firstRadius + dFirst;
firstRectF.right = centerX + firstRadius - dFirst;
firstRectF.top = centerY - firstRadius + dFirst;
firstRectF.bottom = centerY + firstRadius - dFirst;
secondRectF.left = centerX - secondRadius + dSecond;
secondRectF.right = centerX + secondRadius - dSecond;
secondRectF.top = centerY - secondRadius + dSecond;
secondRectF.bottom = centerY + secondRadius - dSecond;
canvas.drawArc(maxRectF, 0, 360 , false, maxProgressPaint);
float firstAngle = (float) 360 * firstProgress / (float) maxProgress;
float secondAngle = ((float) 360 * secondProgress / (float) maxProgress);
float dotAngle = (float) (Math.PI*secondAngle/180.0F);
canvas.drawArc(firstRectF, 0 - 90, firstAngle, false, firstProgressPaint);
canvas.drawArc(secondRectF, 0 - 90, secondAngle , false, secondProgressPaint);
float dotCx = (float) (width*0.5 + (width - dotDiameter)*0.5 * Math.sin(dotAngle));
float dotCy = (float) (height*0.5 - (height -dotDiameter) *0.5 * Math.cos(dotAngle));
if(canDisplayDot){
canvas.drawCircle(dotCx, dotCy, dotDiameter * 0.5F, dotPaint);
}
}
简书地址