編輯: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() 則透過固定步進逐步逼近目標值,讓弧長變化平順而不跳動。這樣就完成了。

Android 取得並管理正在執行的應用程式
因為需要在 framework 層加入類似「工作管理員」的功能,我先寫了一個小 App 來驗證可行性:列出目前執行中的應用,點擊可切換回去,長按則可以結束該程式。 這個
Android 使用 XML 打造動畫 UI 的深入解析
用 XML 定義動畫是 Android 中相當直覺的做法,能把淡入、淡出、縮放、旋轉等效果寫成資源檔,再在程式裡載入套用。本文從建立動畫資料夾開始,逐步說明載入、監聽與觸
用 Handler 實現 ViewPager 廣告頁自動輪播的完整方案
很多電商網站和 App 都有自動輪播的商品推廣圖,看起來體驗很不錯。正好今天在學習 ViewPager,於是就動手做了一個功能類似的 Demo。 下面是其中兩個效果的截圖
打造 3D 翻轉動畫:Rotate3dAnimation 函數剖析
上圖就是最終的翻轉效果。由於 Android 並未提供現成的 3D 翻轉動畫,我們得自己動手實作。先來分析 Animation 與 Transformation 這兩個