編輯:關於 Android 程式設計
The mark of the immature man is that he wants to die nobly for a cause, while the mark of the mature man is that he wants to live humbly for one.
系統自帶的 ProgressBar 樣式有限,很多時候我們需要各種奇怪方向的進度條。好消息是,借助 shape 加 clip 這對組合,幾乎所有形態都能拼出來。下面按方向盤點七種。

1-1 定義要顯示的圖形,drawable 目錄下 progress_v_01.xml:
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:width="500dp" android:height="50dp"/>
<solid android:color="#6633CC"/>
<corners android:radius="20dp"/>
</shape>

1-2 用 clip 標簽包一層,clip_left_01.xml:
<clip xmlns:android="https://schemas.android.com/apk/res/android"
android:clipOrientation="horizontal"
android:gravity="left"
android:drawable="@drawable/progress_v_01"/>
clipOrientation 指定水平方向,gravity 設為 left 決定從左往右生長,drawable 引用要裁剪的源圖形。
1-3 布局中引用:
<ImageView
android:id="@+id/iv_image_clip_left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#e6e6e6"
android:src="@drawable/clip_left_01"/>
1-4 代碼中動態設置進度:
private ImageView mClipLeftImageView;
private int mUnmber = 0;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mClipLeftImageView = (ImageView) findViewById(R.id.iv_image_clip_left);
mClipLeftImageView.setImageLevel(10000);
handler.postDelayed(runnable, 2000);
}
Runnable runnable = new Runnable() {
@Override
public void run() {
if (mUnmber <= 10000) {
mClipLeftImageView.getDrawable().setLevel(mUnmber);
handler.postDelayed(runnable, 20);
mUnmber += 100;
}
}
};

源圖形沿用 1-1 的 progress_v_01,只需把 clip 的 gravity 改成 right:
<clip xmlns:android="https://schemas.android.com/apk/res/android"
android:clipOrientation="horizontal"
android:gravity="right"
android:drawable="@drawable/progress_v_01"/>
布局引用與代碼設置同 1-3、1-4,不再重復。

gravity 換成 center_horizontal,進度就從中線向左右兩邊生長:
<clip xmlns:android="https://schemas.android.com/apk/res/android"
android:clipOrientation="horizontal"
android:gravity="center_horizontal"
android:drawable="@drawable/progress_v_01"/>
布局與代碼同前。

4-1 定義圓形圖形,progress_oval_01.xml:
<shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="oval">
<size android:width="100dp" android:height="100dp"/>
<gradient
android:type="radial"
android:gradientRadius="45"
android:startColor="#FFFF33"
android:centerColor="#CC9933"
android:endColor="#33FF33"
android:centerX="0.5"
android:centerY="0.5"/>
<stroke android:width="2dp" android:color="#0000CC"/>
</shape>
4-2 建立 clip,gravity 設 left,效果為自左向右填充:
<clip xmlns:android="https://schemas.android.com/apk/res/android"
android:clipOrientation="horizontal"
android:gravity="left"
android:drawable="@drawable/progress_oval_01"/>
布局與代碼同 1-3、1-4。

沿用 4-1 的圓形,clip 的 gravity 改為 center:
<clip xmlns:android="https://schemas.android.com/apk/res/android"
android:clipOrientation="horizontal|vertical"
android:gravity="center"
android:drawable="@drawable/progress_oval_01"/>
布局與代碼同前。

6-1 定義細長的矩形圖形:
<shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:width="5dp" android:height="100dp"/>
<gradient
android:angle="45"
android:startColor="#FF9900"
android:centerColor="#FFFF00"
android:endColor="#66FF00"/>
<corners android:radius="5dp"/>
</shape>
效果圖:

6-2 建立 clip,方向改 vertical,gravity 設 bottom,進度便由下往上漲:
<clip xmlns:android="https://schemas.android.com/apk/res/android"
android:clipOrientation="vertical"
android:gravity="bottom"
android:drawable="@drawable/progress_v_02"/>
若把 gravity 換成 top,進度就變成從上往下。布局與代碼同 1-3、1-4。

思路完全一致:4-1 的圓形源圖形,配一個 clipOrientation="vertical"、gravity="bottom" 的 clip 即可。
七種效果說到底都圍繞 ClipDrawable 打轉。它的作用是在另一張 Drawable 上「截取一個片段」,XML 裡用 <clip> 標簽定義,有三個關鍵屬性:
使用時調用 setLevel(int level) 控制截取區域大小:level 為 0 時片段為空,為 10000 時顯示整張圖。於是只要讓程序週期性地遞增 level,圖片就會呈現「漸漸展開」的動畫——這正是上面所有示例在 Runnable 裡反復調 setLevel 的原因。
掌握了這條主線,再遇到任何形狀與方向的進度條需求,本質上都是在換 shape 的 outer shape 與 clip 的 gravity 組合而已。
Android 五大版面配置詳解(附完整範例)
寫 Android 介面,第一步永遠是選對版面配置。本文整理最常用的五種,逐一說明特性、常用屬性並附上可運行的範例。 線性版面(LinearLayout):依水平或垂直方
Zygote 源碼解析:Android 應用進程的起點
什麼是 ZygoteZygote 是 Android 中非常關鍵的一個進程。大多數應用程式進程,以及系統服務所在的 System 進程,都是由 Zygote 負責創建。由
深入解析 Android 中的 Builder 建造者模式
在 Android 原始碼中,設計模式無所不在。其中,建造者模式因為能優雅地封裝複雜物件的建立流程,經常出現在各種框架與元件中。本文以 AlertDialog 為例,說明
關於 Android Bitmap,你可能忽略的那些細節
這篇整理一下 Android 中 Bitmap 使用時容易踩坑的幾個環節,從圖片格式、記憶體計算一路談到加載大圖與修改圖片。 一、電腦表示圖形的幾種方式 1)BMP:幾乎