布局xml
<!-- 自定义圆形进度条 --> <com.mypackage.CircleProgressBarView android:id="@+id/circle_progressbar" android:layout_width="wrap_content" android:layout_height="130dp" />
public class CircleProgressBarView extends View { private int progress; private int max; 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); paint.setAntiAlias(true); //设置是否抗锯齿、 paint.setFlags(Paint.ANTI_ALIAS_FLAG); //消除锯齿 paint.setColor(Color.GRAY); paint.setStrokeWidth(10); //设置画笔宽度 paint.setStyle(Paint.Style.STROKE); //设置中空的样式 //在中心为(100,100)的地方画个半径为50的圆,宽度为setStrokeWidth:10,也就是灰色的底边 canvas.drawCircle(100, 100, 50, paint); paint.setColor(getResources().getColor(R.color.green)); //不用系统的颜色 // set(45, 45, 155, 155)设置类似于左上角坐标(45,45),右下角坐标(155,155),这样也就保证了半径为55 oval.set(50, 50, 150, 150); //画圆弧,第二个参数为:起始角度,第三个为跨的角度,第四个为true的时候是实心,false的时候为空心 canvas.drawArc(oval, -90, ((float)progress / max) * 360, false, paint); paint.reset(); //将画笔重置 paint.setStrokeWidth(3); //再次设置画笔的宽度 paint.setTextSize(30); //设置文字的大小 paint.setColor(Color.CYAN); if(progress == max){ canvas.drawText("完成", 70, 110, paint); }else{ canvas.drawText(progress + "%", 70, 110, paint); } } }