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

Android 自訂進度條完整指南:換色、縱向與弧形

編輯:關於 Android 程式設計

Android 原生的 ProgressBar 只有橫向一種樣式,而且預設外觀實在算不上美觀,如下圖:

當設計稿要求換掉前後背景、改成縱向,甚至做成弧形時,該怎麼辦?本文就帶你從源碼出發,一步步打造客製化的進度條,例如這種風格:

一、更換前景與背景

先看一個典型的 ProgressBar 屬性設定:

<ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:indeterminate="false"
            android:padding="2dip"
            android:progress="50" />

循著它,我們在源碼的 style.xml 找到樣式定義:

<style name="Widget.ProgressBar.Horizontal">
        <item name="android:indeterminateOnly">false</item>
        <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
        <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
        <item name="android:minHeight">20dip</item>
        <item name="android:maxHeight">20dip</item>
    </style>

關鍵是這一行:

<item name="android:progressDrawable">@android:drawable/progress_horizontal</item>

繼續往下挖,在 drawable 目錄找到 progress_horizontal.xml,這就是我們今天的主角:

<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
     <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient android:startColor="#ff9d9e9d" android:centerColor="#ff5a5d5a" android:centerY="0.75" android:endColor="#ff747674" android:angle="270" />
        </shape>
    </item>
     <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
            <gradient android:startColor="#80ffd300" android:centerColor="#80ffb600" android:centerY="0.75" android:endColor="#a0ffcb00" android:angle="270" />
            </shape>
        </clip>
    </item>
     <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
            <gradient android:startColor="#ffffd300" android:centerColor="#ffffb600" android:centerY="0.75" android:endColor="#ffffcb00" android:angle="270" />
            </shape>
        </clip>
    </item>
</layer-list>

看到 @android:id/progress 與 @android:id/secondaryProgress 了嗎?把這個檔案複製到自己的 drawable 目錄,就能自由修改 shape 的漸層、圓角等屬性。

那如果想放一張圖片進去呢?新建 progress_horizontal1.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
     <item android:id="@android:id/progress" android:drawable="@drawable/progressbar" />
</layer-list>

在 android:drawable 中指定你處理好的圖片,回到版面設定前景與背景:

<ProgressBar
            android:id="@+id/progressBar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/progress_bg"
            android:indeterminate="false"
            android:maxHeight="20dip"
            android:minHeight="20dip"
            android:padding="2dip"
            android:progress="50"
            android:progressDrawable="@drawable/progress_horizontal1" />

其中 android:background 指定背景,android:progressDrawable 使用剛才的 progress_horizontal1,設定就完成了。

仔細看會發現四角仍帶有圓角,這是系統自動套上的,即使你的圖片本身沒做圓角處理。

二、縱向進度條

同樣從源碼的 progress_horizontal.xml 著手:

<item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
            <gradient android:startColor="#ffffd300" android:centerColor="#ffffb600" android:centerY="0.75" android:endColor="#ffffcb00" android:angle="270" />
            </shape>
        </clip>
    </item>

為什麼 shape 外層要包一個 clip?官方文件指出 clip 對象具備可複製性。其定義如下:

<clip xmlns:android="https://schemas.android.com/apk/res/android" android:drawable="@drawable/drawable_resource" android:clipOrientation=["horizontal" | "vertical"] android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" | "fill_vertical" | "center_horizontal" | "fill_horizontal" | "center" | "fill" | "clip_vertical" | "clip_horizontal"] />

其中 android:clipOrientation 有水平與垂直兩種取值,預設為 horizontal;android:gravity 預設為 left。那我們試著改成 vertical 與 bottom 看看。新建 progress_vertical.xml,去掉 secondaryProgress,並把 clip 與漸層中心由 centerY 改為 centerX:

<item android:id="@android:id/progress">
        <clip android:clipOrientation="vertical" android:gravity = "bottom">
            <shape>
                <corners android:radius="5dip" />
            <gradient android:startColor="#ffffd300" android:centerColor="#ffffb600" android:centerX="0.75" android:endColor="#ffffcb00" android:angle="90" />
            </shape>
        </clip>
    </item>

版面中指定 android:progressDrawable="@drawable/progress_vertical" 即可。

三、弧形進度條

嚴格說這已經不太算傳統意義的進度條,比較像儀表盤,用處相對有限。它不是改改源碼就能搞定,得靠自訂 View 繪製。先來看成員變數:

public class Arcs extends View {
    private Paint mArcPaint;
    private Paint mArcBGPaint;
    private RectF mOval;
    private float mSweep = 0;
    private int mSpeedMax = 200;
    private int mThreshold = 100;
    private int mIncSpeedValue = 0;
    private int mCurrentSpeedValue = 0;
    private float mCenterX;
    private float mCenterY;
    private float mSpeedArcWidth;
    private final float SPEED_VALUE_INC = 2;
}

兩個 Paint 分別負責前景弧與背景弧,RectF 決定弧線繪製的範圍,其餘則是 sweep 掃過角度、中心座標等控制參數。

接著設定兩支畫筆的顏色、寬度與樣式,並用 BlurMaskFilter 加上邊緣模糊效果:

mArcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mArcPaint.setStyle(Paint.Style.STROKE);
    mArcPaint.setStrokeWidth(mSpeedArcWidth);
    mArcPaint.setColor(0xff81ccd6);
    BlurMaskFilter mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.INNER);
    mArcPaint.setMaskFilter(mBlur);
    mArcBGPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mArcBGPaint.setStyle(Paint.Style.STROKE);
    mArcBGPaint.setStrokeWidth(mSpeedArcWidth+8);
    mArcBGPaint.setColor(0xff171717);
    BlurMaskFilter mBGBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.INNER);
    mArcBGPaint.setMaskFilter(mBGBlur);

重寫 onSizeChanged,依實際版面大小自行做置中運算:

@Override
protected void onSizeChanged(int w, int h, int ow, int oh) {
        super.onSizeChanged(w, h, ow, oh);
        mCenterX = w * 0.5f;
        mCenterY = h - mSpeedArcWidth;
        mOval = new RectF(mCenterX - mCenterY, mSpeedArcWidth, mCenterX + mCenterY, mCenterY * 2);
    }

再重寫 onDraw 進行重繪:

@Override
protected void onDraw(Canvas canvas) {
        drawSpeed(canvas);
        calcSpeed();
}

在 drawSpeed 中,透過 mSweep = (float) mIncSpeedValue / mSpeedMax * 180; 計算掃過角度,再呼叫 canvas.drawArc(mOval, 180, mSweep, false, mArcPaint); 依 mSweep 變化畫出對應長度的弧線,並依是否超過閾值切換顏色:

if (mIncSpeedValue > mThreshold) {
   mArcPaint.setColor(0xFFFF0000);
}
else {
   mArcPaint.setColor(0xFF00B0F0);
}

calcSpeed 則透過固定步進控制增減,讓弧線平滑過渡、避免跳動,最後呼叫 invalidate() 觸發重繪。如此一來,弧形進度條就大功告成了。

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