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

仿應用寶下載進度條:滑塊流光與文字變色的自定義 View 實作

編輯:關於 Android 程式設計

一、成品長什麼樣

一個橫向進度條,已完成區域上有一道滑塊從左往右持續移動(當初就是被這個效果吸引,才想弄清楚它怎麼做的);進度條中央顯示百分比,而且文字落在已下載區域的部分是白色;點擊可暫停,此時進度條顏色與提示文字都會改變。

二、拆解兩個真正的難點

邊框、進度填充這些都很直觀,值得先想清楚的是這兩件事。

1. 讓文字在已下載區域顯示為白色

這與歌詞逐字變色的原理相同。Canvas 提供 save() 搭配 CLIP_SAVE_FLAG 標誌,其含義是「restore() 被呼叫時還原当前的 clip 區域」;配合 clipRect() 與 restore(),就能只對部分區域重繪文字。

2. 讓滑塊在已完成區域內移動

直覺做法是畫一個滑塊(其實就是一張圖片),再依進度不斷改變它的位置。要注意它的移動特性:滑塊從右邊界進入、最後由左邊界離開,而且只在下載完成區域內可見,超出右邊界的部分必須被裁掉。這正是兩個圖層重疊時的合成問題,對應的 Porter-Duff 模式是 SRC_ATOP。

三、實作

1. 自定義屬性


    
    
    

private void initAttrs(AttributeSet attrs) {
    if (attrs != null) {
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.FlikerProgressBar);
        textSize    = (int) ta.getDimension(R.styleable.FlikerProgressBar_textSize, dp2px(12));
        loadingColor = ta.getColor(R.styleable.FlikerProgressBar_loadingColor, Color.parseColor("#40c4ff"));
        stopColor   = ta.getColor(R.styleable.FlikerProgressBar_stopColor, Color.parseColor("#ff9800"));
        ta.recycle();
    }
}

2. onMeasure:wrap_content 時給一個合理預設高度

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int widthSpecSize  = MeasureSpec.getSize(widthMeasureSpec);
    int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

    int height;
    switch (heightSpecMode) {
        case MeasureSpec.AT_MOST:
            height = (int) dp2px(DEFAULT_HEIGHT_DP);
            break;
        case MeasureSpec.EXACTLY:
        case MeasureSpec.UNSPECIFIED:
        default:
            height = heightSpecSize;
            break;
    }
    setMeasuredDimension(widthSpecSize, height);
}

3. onDraw 的四個步驟

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    drawBorder(canvas);          // 1. 邊框
    drawProgress();              // 2. 進度(含滑塊)
    canvas.drawBitmap(pgBitmap, 0, 0, null);
    drawProgressText(canvas);    // 3. 進度文字
    drawColorProgressText(canvas); // 4. 文字變色
}

邊框就是一個 STROKE 風格的矩形。

private void drawBorder(Canvas canvas) {
    bgPaint.setStyle(Paint.Style.STROKE);
    bgPaint.setColor(progressColor);
    bgPaint.setStrokeWidth(dp2px(1));
    canvas.drawRect(0, 0, getWidth(), getHeight(), bgPaint);
}

進度部分先建立一張與已完成區域同寬的 Bitmap 當作畫布,塗滿進度色,再用 SRC_ATOP 把滑塊圖疊上去——這樣滑塊只會出現在已完成區域內。

private void drawProgress() {
    bgPaint.setStyle(Paint.Style.FILL);
    bgPaint.setStrokeWidth(0);
    bgPaint.setColor(progressColor);

    float right = (progress / MAX_PROGRESS) * getMeasuredWidth();
    pgBitmap = Bitmap.createBitmap((int) Math.max(right, 1),
                                   getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    pgCanvas = new Canvas(pgBitmap);
    pgCanvas.drawColor(progressColor);

    if (!isStop) {
        bgPaint.setXfermode(xfermode);   // SRC_ATOP
        pgCanvas.drawBitmap(flikerBitmap, flickerLeft, 0, bgPaint);
        bgPaint.setXfermode(null);
    }
}

文字先用進度色畫一遍,再用白色重畫一次,但這次限制在裁剪區域內。

private void drawProgressText(Canvas canvas) {
    textPaint.setColor(progressColor);
    progressText = getProgressText();
    textPaint.getTextBounds(progressText, 0, progressText.length(), textBouds);

    float xCoordinate = (getMeasuredWidth() - textBouds.width()) / 2;
    float yCoordinate = (getMeasuredHeight() + textBouds.height()) / 2;
    canvas.drawText(progressText, xCoordinate, yCoordinate, textPaint);
}

private void drawColorProgressText(Canvas canvas) {
    textPaint.setColor(Color.WHITE);
    float xCoordinate = (getMeasuredWidth() - textBouds.width()) / 2;
    float yCoordinate = (getMeasuredHeight() + textBouds.height()) / 2;
    float progressWidth = (progress / MAX_PROGRESS) * getMeasuredWidth();

    if (progressWidth > xCoordinate) {
        canvas.save(Canvas.CLIP_SAVE_FLAG);
        float right = Math.min(progressWidth, xCoordinate + textBouds.width());
        canvas.clipRect(xCoordinate, 0, right, getMeasuredHeight());
        canvas.drawText(progressText, xCoordinate, yCoordinate, textPaint);
        canvas.restore();
    }
}

四、效果

五、原始碼

完整專案在 GitHub:https://github.com/LineChen/FlickerProgressBar

回頭看,這個控制項最有意思的地方是它把三個獨立的繪圖技巧疊在一起:裁剪處理文字雙色、Xfermode 處理圖層遮罩、自定義屬性處理外觀。任一個單獨看都不難,組合起來就有了成品感。

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