Android圆形进度条

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);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值