Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自訂圓環進度控制項:純程式碼實作百分比顯示

編輯:關於 Android 程式設計

先謝謝公司願意留一點空檔給我,讓我有機會把基礎重新踩實,把一些一直想碰的東西真正摸上一輪。這篇要分享的是一顆自製控制項,接續先前那條「水平漸變進度條」的思路,這次換成圓形的。

先不談實作,看圖感受一下最終長相:

一、先把結構拆開來看

這顆控制項本質上就是「圓圈套圓圈」:最外層是一圈低透明度的底環,往內是進度軌道(灰色底 + 綠色已完成弧段),再往內是一塊不透明白圓充當文字背板,最後在圓心疊上百分比數字與當前時間。

二、進度怎麼算

其實就是小學二年級的數學題:目前進度 ÷ 總量 = 百分比。乘以 100 取整數,就是圓心要顯示的數字。

三、中間那行時間

單純把系統當前時間格式化後貼上去,沒有什麼玄機,就是讓畫面看起來更完整一點。

四、程式碼

package com.spring.progressview;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * 圓圈進度控制項
 */
public class RoundProgressView extends View {

    /** 最外圍環的顏色 */
    private int mOutRoundColor = Color.argb(60, 255, 255, 255);
    /** 中心圓的顏色 */
    private int mCenterRoundColor = Color.argb(255, 255, 255, 255);
    /** 進度弧的顏色 */
    private int mProgressRoundColor = Color.argb(255, 255, 255, 255);
    /** 進度軌道的背景色 */
    private int mProgressRoundBgColor = Color.argb(100, 255, 255, 255);
    /** 進度條寬度 */
    private int mProgressWidth = 5;

    /** 文字顏色 */
    private int mTextColor = Color.rgb(118, 181, 66);
    private float mPencentTextSize = 65;

    private int mWidth, mHeight;
    private int mPaddingX;
    private float mProgress = 0.5f;
    private float mMax = 1.0f;

    /** 時間顯示格式 */
    private SimpleDateFormat mDateFormat = new SimpleDateFormat("HH:mm:ss.S");

    private Paint mPaint = new Paint();

    public RoundProgressView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

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

    public RoundProgressView(Context context) {
        super(context);
        init();
    }

    public void init() {
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mWidth = getWidth();
        mHeight = getHeight();

        // 寬大於高時,以高為準並水平居中
        if (mWidth > mHeight) {
            mPaddingX = (mWidth - mHeight) / 2;
            mWidth = mHeight;
        }

        mPaint.setAntiAlias(true); // 消除鋸齒
        mPaint.setStyle(Style.FILL);
        mPaint.setColor(mOutRoundColor);
        RectF oval = new RectF(new Rect(mPaddingX, 0, mWidth + mPaddingX, mHeight));
        canvas.drawArc(oval, 0, 360, true, mPaint);

        // 進度軌道
        int halfWidth = mWidth / 6;
        mPaint.setColor(mProgressRoundBgColor);
        mPaint.setStrokeWidth(mProgressWidth);
        mPaint.setStyle(Style.STROKE);
        oval = new RectF(new Rect(halfWidth + mPaddingX, halfWidth,
                halfWidth * 5 + mPaddingX, halfWidth * 5));
        canvas.drawArc(oval, 0, 360, false, mPaint);

        // 進度弧,從 12 點方向開始掃
        mPaint.setColor(mProgressRoundColor);
        canvas.drawArc(oval, 90, 360 * mProgress / mMax, false, mPaint);

        // 中心實心圓
        halfWidth = mWidth / 4;
        mPaint.setStyle(Style.FILL);
        mPaint.setColor(mCenterRoundColor);
        oval = new RectF(new Rect(halfWidth + mPaddingX, halfWidth,
                halfWidth * 3 + mPaddingX, halfWidth * 3));
        canvas.drawArc(oval, 0, 360, false, mPaint);

        // 百分比數字
        mPaint.reset();
        mPaint.setTextSize(mPencentTextSize);
        mPaint.setColor(mTextColor);
        mPaint.setStyle(Style.FILL);
        mPaint.setTextAlign(Align.CENTER);
        String number = (int) (mProgress * 100 / mMax) + "";
        canvas.drawText(number, mWidth / 2 + mPaddingX,
                mHeight / 2 + mPencentTextSize / 3, mPaint);

        // 百分號
        float textWidth = mPaint.measureText(number);
        mPaint.setTextSize(mPencentTextSize / 2);
        canvas.drawText("%", mWidth / 2 + mPaddingX + textWidth / 2 + 5,
                mHeight / 2 - mPencentTextSize / 8, mPaint);

        // 當前時間
        mPaint.setTextSize(mPencentTextSize / 2);
        canvas.drawText(mDateFormat.format(new Date(System.currentTimeMillis())),
                mWidth / 2 + mPaddingX, mHeight / 2 + halfWidth / 4 * 3, mPaint);
    }

    public void setMax(float mMax) {
        this.mMax = mMax;
    }

    public void setProgress(float mProgress) {
        this.mProgress = mProgress;
        invalidate();
    }

    public float getMax() {
        return mMax;
    }

    public float getProgress() {
        return mProgress;
    }
}

五、幾個值得留意的細節

1. 顏色與透明度。Color.argb(60, 255, 255, 255) 的第一個參數是 alpha,範圍 0~255,數值越小越透明;後三個才是 RGB。想讓外環「淡」下來,就是靠這裡。

2. 空心圓怎麼畫。關鍵是這兩行:

mPaint.setStyle(Style.STROKE); // 只描邊,不填色

mPaint.setStrokeWidth(mProgressWidth); // 描邊寬度

搭配 drawArc,就能畫出一條寬度固定為 mProgressWidth 的圓形軌跡。如果不設定 STROKE,畫出來的會是一顆被填滿的實心圓。

3. 起止角度要算準。drawArc(oval, startAngle, sweepAngle, useCenter, paint) 中,第二個參數是起始角度,第三個是掃過的角度。進度弧從 90 度(12 點鐘方向)起算,掃過 360 × progress / max,才會呈現「順時針填滿」的視覺。

4. 文字位置需要微調。Canvas 的文字基線並不等於視覺中心,所以 y 座標通常要手動加一個偏移量(這裡用 mPencentTextSize / 3)來讓數字看起來真正置中。

若還有不熟悉的地方,建議直接翻 Canvas 與 Paint 的 API 文件,這兩個類才是自訂 View 的核心。

六、寫這篇的目的

主要是想分享一點思路。如果你手上也有寫得順手、可重複使用的好控制項,也希望大方丟出來,讓整個社群少踩幾次坑。

Demo 的部分我就不直接把連結貼在正文裡了,避免被爬蟲掃走;下載位置請看程式碼上方的註解說明。

轉載請保留作者出處:Spring sky。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved