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

自定義一個能顯示下載進度的 Button

編輯:關於 Android 程式設計

不少應用在觸發下載後不會另開進度條,而是直接在按鈕上呈現進度:背景色自左向右填充,填滿即代表完成,交互輕巧又不打斷操作。

另一種常見做法來自視頻客戶端:在按鈕四周畫一圈描邊,四條邊各代表 25%,從上邊開始順時針推進,回到左上角即 100%。兩種樣式都很好看,於是自己寫了一個自定義 Button 把二者都實現出來。

填充式進度:

描邊式進度:

實現思路

繼承 Button 並重寫 onDraw(),兩種樣式在裡面分支處理。填充式相對簡單:

if (currentType == TYPE_FILL) {
    mPaint.setColor(getContext().getResources().getColor(R.color.green_yellow));
    mPaint.setAntiAlias(true);
    mPaint.setAlpha(128);
    mPaint.setStrokeWidth(1.0f);

    Rect rect = new Rect();
    canvas.getClipBounds(rect);
    rect.left += getPaddingLeft();
    rect.top += getPaddingTop();
    // 右邊界由當前進度推算
    rect.right = (rect.left - getPaddingLeft())
            + (mProgress * getWidth() / 100) - getPaddingRight();
    rect.bottom -= getPaddingBottom();

    // 圓角長條視覺上更柔和
    canvas.drawRoundRect(new RectF(rect), 8.0f, 8.0f, mPaint);
}

描邊式麻煩一些,本質上就是把矩形拆成四段細長條,按進度分段繪制:

else if (currentType == TYPE_STROKE) {
    mPaint.setAntiAlias(true);
    mPaint.setColor(getContext().getResources().getColor(R.color.green_yellow));
    mPaint.setAlpha(255);

    Rect rect = new Rect();
    canvas.getClipBounds(rect);

    if (mProgress >= 0 && mProgress < 25) {
        // 只畫上邊的一段
        Rect top = new Rect(rect.left + getPaddingLeft(),
                rect.top + getPaddingTop(),
                rect.left + mProgress
                        * (getWidth() - getPaddingLeft() - getPaddingRight()) / 25
                        - getPaddingRight(),
                rect.top + getPaddingTop() + 2);
        canvas.drawRect(top, mPaint);
    } else if (mProgress < 50) {
        Rect top = new Rect(rect.left + getPaddingLeft(),
                rect.top + getPaddingTop(),
                rect.right - getPaddingRight(),
                rect.top + getPaddingTop() + 2);
        canvas.drawRect(top, mPaint);

        Rect right = new Rect(rect.right - getPaddingRight() - 2,
                rect.top + getPaddingTop(),
                rect.right - getPaddingRight(),
                rect.top + getPaddingTop() + (mProgress - 25)
                        * (getHeight() - getPaddingTop() - getPaddingBottom()) / 25);
        canvas.drawRect(right, mPaint);
    } else if (mProgress < 75) {
        Rect top = new Rect(rect.left + getPaddingLeft(),
                rect.top + getPaddingTop(),
                rect.right - getPaddingRight(),
                rect.top + getPaddingTop() + 2);
        canvas.drawRect(top, mPaint);

        Rect right = new Rect(rect.right - getPaddingRight() - 2,
                rect.top + getPaddingTop(),
                rect.right - getPaddingRight(),
                rect.bottom - getPaddingBottom());
        canvas.drawRect(right, mPaint);

        Rect bottom = new Rect(
                rect.right - getPaddingRight() - (mProgress - 50)
                        * (getWidth() - getPaddingLeft() - getPaddingRight()) / 25,
                rect.bottom - getPaddingBottom() - 2,
                rect.right - getPaddingRight(),
                rect.bottom - getPaddingBottom());
        canvas.drawRect(bottom, mPaint);
    } else if (mProgress <= 100) {
        Rect top = new Rect(rect.left + getPaddingLeft(),
                rect.top + getPaddingTop(),
                rect.right - getPaddingRight(),
                rect.top + getPaddingTop() + 2);
        canvas.drawRect(top, mPaint);

        Rect right = new Rect(rect.right - getPaddingRight() - 2,
                rect.top + getPaddingTop(),
                rect.right - getPaddingRight(),
                rect.bottom - getPaddingBottom());
        canvas.drawRect(right, mPaint);

        Rect bottom = new Rect(rect.left + getPaddingLeft(),
                rect.bottom - getPaddingBottom() - 2,
                rect.right - getPaddingRight(),
                rect.bottom - getPaddingBottom());
        canvas.drawRect(bottom, mPaint);

        Rect left = new Rect(rect.left + getPaddingLeft(),
                rect.bottom - getPaddingBottom() - (mProgress - 75)
                        * (getHeight() - getPaddingTop() - getPaddingBottom()) / 25,
                rect.left + getPaddingLeft() + 2,
                rect.bottom - getPaddingBottom());
        canvas.drawRect(left, mPaint);
    }
}

最後別忘了調用 super.onDraw(canvas),這樣自定義的填充或描邊會繪製在原有內容的底層,不會遮住按鈕上的文字與圖標。

對外暴露更新進度的接口

public void updateProgress(int progress) {
    if (progress >= 0 && progress <= 100) {
        mProgress = progress;
    } else if (progress < 0) {
        mProgress = 0;
    } else {
        mProgress = 100;
    }
    invalidate();
}

把越界值夾到 [0, 100] 區間再刷新,能避免外部傳入異常數值時出現繪制錯位。

Demo:https://github.com/YoungLeeForeverBoy/ProgressButton

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