要在环形ProgressBar上显示进度百分比,可以通过设置ProgressBar的样式和自定义绘制来实现。以下是一个示例代码,演示如何在环形ProgressBar上显示进度百分比:
public class CircleProgressBar extends View {
private Paint mPaint;
private RectF mRectF;
private int mProgress;
private int mMaxProgress;
private String mProgressText;
public CircleProgressBar(Context context) {
super(context);
init();
}
public CircleProgressBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public CircleProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(10);
mRectF = new RectF();
mMaxProgress = 100;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
int radius = Math.min(centerX, centerY) - 5;
mRectF.set(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
mPaint.setColor(Color.GRAY);
canvas.drawCircle(centerX, centerY, radius, mPaint);
mPaint.setColor(Color.BLUE);
canvas.drawArc(mRectF, -90, 360 * mProgress / mMaxProgress, false, mPaint);
mPaint.setTextSize(50);
mProgressText = mProgress + "%";
float textWidth = mPaint.measureText(mProgressText);
canvas.drawText(mProgressText, centerX - textWidth / 2, centerY + mPaint.getTextSize() / 2, mPaint);
}
public void setProgress(int progress) {
mProgress = progress;
invalidate();
}
}
在上面的代码中,我们继承了View类,并重写了onDraw方法,通过绘制圆形和弧线来实现环形ProgressBar的效果。同时,在ProgressBar的中心绘制了进度百分比的文本。
要在Activity中使用这个自定义的CircleProgressBar,可以像下面这样:
CircleProgressBar progressBar = findViewById(R.id.circleProgressBar);
progressBar.setProgress(50); // 设置进度为50%
通过调用setProgress方法,可以设置ProgressBar的进度,进度百分比将会显示在环形ProgressBar的中心位置。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。