您好,登錄后才能下訂單哦!
要在環(huán)形ProgressBar上顯示進(jìn)度百分比,可以通過(guò)設(shè)置ProgressBar的樣式和自定義繪制來(lái)實(shí)現(xiàn)。以下是一個(gè)示例代碼,演示如何在環(huán)形ProgressBar上顯示進(jìn)度百分比:
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類(lèi),并重寫(xiě)了onDraw方法,通過(guò)繪制圓形和弧線來(lái)實(shí)現(xiàn)環(huán)形ProgressBar的效果。同時(shí),在ProgressBar的中心繪制了進(jìn)度百分比的文本。
要在Activity中使用這個(gè)自定義的CircleProgressBar,可以像下面這樣:
CircleProgressBar progressBar = findViewById(R.id.circleProgressBar);
progressBar.setProgress(50); // 設(shè)置進(jìn)度為50%
通過(guò)調(diào)用setProgress方法,可以設(shè)置ProgressBar的進(jìn)度,進(jìn)度百分比將會(huì)顯示在環(huán)形ProgressBar的中心位置。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。