android环状显示百分比、环状百分比视图实现

本文介绍如何自定义一个Android视图来显示带有百分比的圆形进度条,并附带下方的文字说明。通过重写onDraw方法实现进度条的绘制,包括背景圆环、进度圆环、中间填充圆及百分比数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图:

百分比下带文字

只显示百分比

实现:

自定义一个View,重写onDraw方法:

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mWidth = getWidth();
        mHeight = getHeight();

        if(mWidth > mHeight){
            mWidth = mHeight;
        }
        mPaint.setAntiAlias(true); // 消除锯齿

        int halfWidth = mWidth/6;
        mPaint.setStrokeWidth(mProgressWidth);
        mPaint.setColor(mProgressRoundBgColor);
        mPaint.setStyle(Paint.Style.STROKE);
        RectF oval = new RectF(new Rect(halfWidth+mPaddingX, halfWidth, halfWidth*5+mPaddingX, halfWidth*5));
        canvas.drawArc(oval, 0, 360, false, mPaint);

        mPaint.setColor(mProgressRoundColor);
        canvas.drawArc(oval, -90, -360*mProgress/mMax, false, mPaint);


        halfWidth = mWidth/5;
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mCenterRoundColor);
        oval = new RectF(new Rect(halfWidth+mPaddingX, halfWidth, halfWidth*4+mPaddingX, halfWidth*4));
        canvas.drawArc(oval, 0, 360, false, mPaint);

        mPaint.reset();
        if(TextUtils.isEmpty(mBelowText)) {
            mPaint.setTextSize(mPencentTextSize);
            mPaint.setColor(mTextColor);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setTextAlign(Paint.Align.CENTER);
            String number = String.format("%.0f", mProgress * 100 / mMax);
            canvas.drawText(number, mWidth / 2 + mPaddingX - 25, (mHeight / 2 + mPencentTextSize / 3), mPaint);

            float textWidth = mPaint.measureText(number);
            mPaint.setTextSize(mPencentTextSize);
            canvas.drawText("%", mWidth / 2 + mPaddingX + textWidth / 2 + 10, (mHeight / 2 + mPencentTextSize / 3), mPaint);
        }else{
            mPaint.setTextSize(mPencentTextSize);
            mPaint.setColor(mTextColor);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setTextAlign(Paint.Align.CENTER);
            String number = String.format("%.0f", mProgress * 100 / mMax);
            canvas.drawText(number, mWidth / 2 + mPaddingX - 25, (mHeight / 2 + mPencentTextSize / 3)-20, mPaint);
            mPaint.setColor(Color.parseColor("#C2C2C2"));
            mPaint.setTextSize(DensityUtil.dip2px(mContext,12f));
            canvas.drawText(mBelowText,(mWidth / 2 + mPaddingX - 25)+23,(mHeight / 2 + mPencentTextSize / 3)+20,mPaint);

            float textWidth = mPaint.measureText(number);
            mPaint.setTextSize(mPencentTextSize);
            mPaint.setColor(mTextColor);
            canvas.drawText("%", mWidth / 2 + mPaddingX + textWidth / 2 + 10, (mHeight / 2 + mPencentTextSize / 3)-20, mPaint);
        }
    }

源码及使用代码下载:

https://download.youkuaiyun.com/download/yonghuming_jesse/10677919

      最后推荐给一些想进大厂或者还没有拿到心仪offer的攻城狮们一本书,由大厂java面试官胡书敏编写,满满的干货,助你进到想去的公司。

 

博主上传资源下载链接:

自制免费无广告小说阅读APP下载:

https://download.youkuaiyun.com/download/yonghuming_jesse/10390364

全屏播放视频不拉伸源码:

https://download.youkuaiyun.com/download/yonghuming_jesse/10646274

科大讯飞语音评测服务接入源码:

https://download.youkuaiyun.com/download/yonghuming_jesse/10616924

android饺子播放器使用源码:

https://download.youkuaiyun.com/download/yonghuming_jesse/10619119

视频播放前显示视频第一帧源码:

https://download.youkuaiyun.com/download/yonghuming_jesse/10646332

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JesseAndroid

每一份支持都是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值