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

自定義 View 仿 QQ 等級:成長天數進度控件

編輯:關於 Android 程式設計

自定義 View 這塊持續看了一週多,每天更新一篇,慢慢摸到一些手感,對「怎麼算」這件事也有了更深的認識。今天在手機上看到一個成長天數的進度控件,覺得有趣,就自己動手做了一個。

畫面元素拆開來並不複雜:一個圓角矩形背景、兩段環形、三段文字。逐個實現即可。

先畫背景矩形:認識 RectF

RectF 用四個單精度浮點座標(left、top、right、bottom)描述一個矩形,屬性可直接讀寫,另有 width() 與 height() 取得寬高。它提供四個構造方法:

  • RectF():建立空矩形。
  • RectF(float left, float top, float right, float bottom):以四個座標建立。
  • RectF(RectF r):以既有 RectF 建立(左上角座標不變)。
  • RectF(Rect r):以既有 Rect 建立。

這裡用第二個,在 onSizeChanged() 中依控制項實際尺寸算出四周留白 10% 的範圍:

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mWidth = getWidth();
    mHeight = getHeight();
    mRectF = new RectF((float) (mWidth * 0.1), (float) (mHeight * 0.1),
            (float) (mWidth * 0.9), (float) (mHeight * 0.9));
}

畫兩段環形

之所以用兩支畫筆、兩段弧,是因為一段當背景軌道(完整 360 度),一段當進度。兩者共用同一個 RectF,只有掃描角度不同:

canvas.drawArc(mRectF, 90, 360, false, mButtomPaint);  // 背景環
canvas.drawArc(mRectF, 15, 180, false, mTopPaint);     // 進度環

畫文字

三段文字(數字、單位、說明)都需要先量寬再定位,才能排得整齊:

canvas.drawText("5.0", (mWidth - (mTextWidth + mTextSmail)) / 2, (float) (mHeight / 2), mTextPaint);
canvas.drawText("天", (mWidth - (mTextWidth + mTextSmail)) / 2 + mTextWidth, (float) (mHeight / 2), mSmailTextPaint);
canvas.drawText("升級年費超級會員立即升至", (mWidth - mTextSmailButtom) / 2, (float) (mHeight / 2 + 30), mSmailTextPaint);

完整程式碼:

public class MyProgress extends View {

    private Paint mButtomPaint;
    private Paint mTopPaint;
    private Paint mTextPaint;
    private Paint mSmailTextPaint;
    private float mWidth;
    private float mHeight;
    private RectF mRectF;

    public MyProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public MyProgress(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MyProgress(Context context) {
        super(context);
        initView();
    }

    private void initView() {
        mButtomPaint = new Paint();
        mButtomPaint.setColor(Color.rgb(69, 142, 253));
        mButtomPaint.setAntiAlias(true);
        mButtomPaint.setStrokeWidth(10);
        mButtomPaint.setStyle(Style.STROKE);

        mTopPaint = new Paint();
        mTopPaint.setColor(Color.parseColor("#ffffff"));
        mTopPaint.setAntiAlias(true);
        mTopPaint.setStrokeWidth(10);
        mTopPaint.setStyle(Style.STROKE);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStrokeWidth(5);
        mTextPaint.setTextSize(50);

        mSmailTextPaint = new Paint();
        mSmailTextPaint.setStrokeWidth(3);
        mSmailTextPaint.setColor(Color.WHITE);
        mSmailTextPaint.setAntiAlias(true);
        mSmailTextPaint.setTextSize(15);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = getWidth();
        mHeight = getHeight();
        mRectF = new RectF((float) (mWidth * 0.1), (float) (mHeight * 0.1),
                (float) (mWidth * 0.9), (float) (mHeight * 0.9));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawArc(mRectF, 90, 360, false, mButtomPaint);
        canvas.drawArc(mRectF, 15, 180, false, mTopPaint);

        float mTextWidth = mTextPaint.measureText("5.0");
        float mTextSmail = mSmailTextPaint.measureText("天");
        float mTextSmailButtom = mSmailTextPaint.measureText("升級年費超級會員立即升至");

        canvas.drawText("5.0", (mWidth - (mTextWidth + mTextSmail)) / 2, (float) (mHeight / 2), mTextPaint);
        canvas.drawText("天", (mWidth - (mTextWidth + mTextSmail)) / 2 + mTextWidth, (float) (mHeight / 2), mSmailTextPaint);
        canvas.drawText("升級年費超級會員立即升至", (mWidth - mTextSmailButtom) / 2, (float) (mHeight / 2 + 30), mSmailTextPaint);
    }
}

要讓它真正可用,建議把「5.0」「天」這些寫死的值抽成 setData(int days) 這類對外方法,並用 ValueAnimator 讓進度環平滑生長;同時把 measureText() 移到 onSizeChanged() 中計算一次就好,因為 onDraw() 的呼叫頻率非常高,能省則省。

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