Android 自定義圓形進(jìn)度條

  1. 創(chuàng)建自定義 View 類
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class CircularProgressBar extends View {
    private Paint backgroundPaint; // 背景大圓(底色)畫筆
    private Paint progressPaint;   // 進(jìn)度小圓(前景)畫筆
    private Paint textPaint;       // 文字畫筆
    private RectF rectF;           // 用于繪制圓弧的矩形區(qū)域
    private float strokeWidth = 20; // 圓環(huán)的寬度
    private float progress = 0;     // 當(dāng)前進(jìn)度
    private int max = 100;          // 最大進(jìn)度值

    public CircularProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backgroundPaint.setColor(0xFFC0C0C0); // 設(shè)置背景圓顏色
        backgroundPaint.setStyle(Paint.Style.STROKE);
        backgroundPaint.setStrokeWidth(strokeWidth);

        progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        progressPaint.setColor(0xFFFF0000); // 設(shè)置進(jìn)度圓顏色
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setStrokeWidth(strokeWidth);

        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(0xFF000000); // 設(shè)置文字顏色
        textPaint.setTextSize(40); // 設(shè)置文字大小
        textPaint.setTextAlign(Paint.Align.CENTER); // 設(shè)置文字居中對(duì)齊

        rectF = new RectF();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        
        float centerX = getWidth() / 2f;
        float centerY = getHeight() / 2f;
        float radius = Math.min(centerX, centerY) - strokeWidth / 2f;

        // 繪制背景圓 (大圓)
        canvas.drawCircle(centerX, centerY, radius, backgroundPaint);

        // 使用 rectF 確定小圓(進(jìn)度圓)的位置和大小
        rectF.set(centerX - radius, centerY - radius, centerX + radius, centerY + radius);

        // 計(jì)算當(dāng)前進(jìn)度對(duì)應(yīng)的角度
        float angle = 360 * progress / max;

        // 繪制進(jìn)度圓(小圓)
        canvas.drawArc(rectF, -90, angle, false, progressPaint);

        // 繪制進(jìn)度文字
        String progressText = String.format("%d%%", (int) (progress / max * 100));
        // 計(jì)算Baseline繪制起點(diǎn)Y坐標(biāo)
        float textY = centerY - (textPaint.descent() + textPaint.ascent()) / 2f;
        canvas.drawText(progressText, centerX, textY, textPaint);
    }

    public void setProgress(float progress) {
        this.progress = progress;
        invalidate(); // 請(qǐng)求重繪視圖
    }

    // ... 可能還需要其他一些方法來設(shè)置屬性 ...
}
  1. 在布局文件中使用自定義進(jìn)度條
<com.example.yourapp.CircularProgressBar
    android:id="@+id/circularProgressBar"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="center"/>

確保將 com.example.yourapp 替換為你的應(yīng)用包名。

  1. 在 Activity 或 Fragment 中更新進(jìn)度
CircularProgressBar circularProgressBar = findViewById(R.id.circularProgressBar);
// 更新進(jìn)度到某個(gè)值
circularProgressBar.setProgress(50); // 假設(shè)進(jìn)度為 50%

該自定義的 CircularProgressBar 類會(huì)繪制一個(gè)大圓作為背景,然后根據(jù)當(dāng)前進(jìn)度在其上方繪制一個(gè)小圓。進(jìn)度條中間還會(huì)顯示當(dāng)前進(jìn)度的百分比。你可以通過調(diào)用 setProgress 方法來更新進(jìn)度,并且此方法會(huì)請(qǐng)求重新繪制視圖以反映新的進(jìn)度。

以上只是一個(gè)基礎(chǔ)示例,你可能需要添加更多功能或?qū)傩?,例如?dòng)態(tài)設(shè)置圓環(huán)顏色、寬度、文本樣式等,以及提供回調(diào)方法用于進(jìn)度完成時(shí)的監(jiān)聽等。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容