Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 自訂進度條完整教學:換色、縱向與圓弧

編輯:Android 開發實例

Android 原生的 ProgressBar 只有橫向一種,樣式也不好改。

實在稱不上好看。當設計稿要求換前景背景、改成縱向、甚至做成圓弧形時,該怎麼辦?例如這種:

我們從原始碼開始拆解。

一、更換前景與背景

先看基本屬性:

<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dip"
    android:layout_toRightOf="@+id/progressBarV"
    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>

關鍵是 android:progressDrawable 指向的 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:layout_below="@+id/progressBar"
    android:layout_margin="5dip"
    android:layout_toRightOf="@+id/progressBarV"
    android:background="@drawable/progress_bg"
    android:indeterminate="false"
    android:indeterminateOnly="false"
    android:maxHeight="20dip"
    android:minHeight="20dip"
    android:padding="2dip"
    android:progress="50"
    android:progressDrawable="@drawable/progress_horizontal1" />

android:background 指定底圖,android:progressDrawable 指定前景,完成。

仔細看四角仍帶有圓角,那是系統自己加的,與你的圖片本身無關。

二、縱向進度條

同樣從 progress_horizontal.xml 出發,重點在 progress 那一段:

<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 是「可自我複製」的,其定義如下:

<?xml version="1.0" encoding="utf-8"?>
<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/vertical 兩種,預設 horizontal;android:gravity 預設 left。把它們改成 vertical 與 bottom 試試,新增 progress_vertical.xml:

<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>

注意 centerY 改成了 centerX、角度改成 90。版面配置中把 android:progressDrawable 指向 @drawable/progress_vertical 即可。

三、圓弧形進度條

這個嚴格說已經不算進度條了,最常見的用途是儀表盤。它也無法靠改 XML 解決,必須自訂 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 決定圓弧繪製的範圍,其餘是掃過角度、中心座標等控制參數。

初始化畫筆

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);

設定顏色、寬度與樣式;BlurMaskFilter 提供邊緣模糊效果,有多種類型可自行嘗試。

依實際尺寸做居中

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

繪製與數值緩衝

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

private void drawSpeed(Canvas canvas) {
    canvas.drawArc(mOval, 179, 181, false, mArcBGPaint);

    mSweep = (float) mIncSpeedValue / mSpeedMax * 180;
    if (mIncSpeedValue > mThreshold) {
        mArcPaint.setColor(0xFFFF0000);
    }
    else {
        mArcPaint.setColor(0xFF00B0F0);
    }

    canvas.drawArc(mOval, 180, mSweep, false, mArcPaint);
}

private void calcSpeed() {
    if (mIncSpeedValue < mCurrentSpeedValue) {
        mIncSpeedValue += SPEED_VALUE_INC;
        if (mIncSpeedValue > mCurrentSpeedValue) {
            mIncSpeedValue = mCurrentSpeedValue;
        }
        invalidate();
    }
    else if (mIncSpeedValue > mCurrentSpeedValue) {
        mIncSpeedValue -= SPEED_VALUE_INC;
        if (mIncSpeedValue < mCurrentSpeedValue) {
            mIncSpeedValue = mCurrentSpeedValue;
        }
        invalidate();
    }
}

核心邏輯:先用 mSweep = mIncSpeedValue / mSpeedMax * 180 換算出掃過角度,再以 drawArc() 畫出對應長度的弧線;並依是否超過閾值切換顏色:

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

calcSpeed() 則透過固定步進逐步逼近目標值,讓弧長變化平順而不跳動。這樣就完成了。

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