您好,登錄后才能下訂單哦!
要在環形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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。