效果图:
百分比下带文字
只显示百分比
实现:
自定义一个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