public class CircleProgressBarView extends View { private int progress; private int max = 100; private Paint paint; private RectF oval; public int getMax() { return max; } public void setMax(int max) { this.max = max; } public int getProgress() { return progress; } public void setProgress(int progress) { this.progress = progress; invalidate(); } public CircleProgressBarView(Context context, AttributeSet attrs) { super(context, attrs); paint = new Paint(); oval = new RectF(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int center = getWidth() / 2; int height = getHeight() / 2; int radius = center / 2; paint.setAntiAlias(true);// 设置是否抗锯齿 paint.setFlags(Paint.ANTI_ALIAS_FLAG);// 帮助消除锯齿 paint.setARGB(255, 4, 64, 87);// 设置画笔灰色 paint.setStrokeWidth(10);// 设置画笔宽度 paint.setStyle(Paint.Style.STROKE);// 设置中空的样式 canvas.drawCircle(center, height, radius, paint);// 在中心为(100,100)的地方画个半径为55的圆,宽度为setStrokeWidth:10,也就是灰色的底边 paint.setColor(Color.WHITE);// 设置画笔为白 oval.set((center - radius), (height - radius), (center + radius), (height + radius));// 设置类似于左上角坐标(45,45),右下角坐标(155,155),这样也就保证了半径为55 canvas.drawArc(oval, -90, ((float) progress / max) * 360, false, paint);// 画圆弧,第二个参数为:起始角度,第三个为跨的角度,第四个为true的时候是实心,false的时候为空心 paint.reset();// 将画笔重置 paint.setStrokeWidth(3);// 再次设置画笔的宽度 paint.setColor(Color.WHITE);// 设置画笔颜色 paint.setTextSize(60); canvas.drawText("备份安全度", (center - 150), (height + 60), paint); paint.setTextSize(100);// 设置文字的大小 if (progress == max) { canvas.drawText(progress + "%", (center - 100), height, paint); } else if (progress < max && progress != 0) { canvas.drawText(progress + "%", (center - 50), height, paint); } } }
Android圆形进度条
最新推荐文章于 2021-05-29 23:12:03 发布