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

Android 自訂進度條:從系統源碼學會客製化 ProgressBar

編輯:關於 Android 程式設計

1. 成品預覽

2. 準備素材

progress1.png(78*78)、progress2.png(78*78)

3. 實作原理

一張圖當背景(通常選淺色),另一張當進度(通常選深色)。進度往前的時候,進度圖逐步揭露、背景圖逐步被覆蓋,視覺上就形成了填充效果。

4. 靈感來自系統控件的源碼

4.1 先看預設的帶進度進度條

 <ProgressBar android:id="@+id/progressBar2" android:layout_width="268dp" android:layout_height="wrap_content" android:progress="45" />

注意:真正起作用的是 style 屬性。

4.2 找到樣式的定義位置

把游標停在 style 屬性值上,按住 Ctrl 會出現超連結,點下去就跳到定義處。

樣式內容如下:

重點研究兩處:android:progressDrawable 指向進度條的樣式;@android:drawable/progress_horizontal 則是樣式定義檔。到 android-sdk-windows\platforms\android-14\data\res 底下搜尋 progress_horizontal.xml:

打開後內容如下:

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

三個 item 分別是:@android:id/background 定義背景樣式、@android:id/secondaryProgress 是輔助進度、@android:id/progress 是主要進度。順帶一提,想要垂直進度條也並不難,關鍵就在 clip 元素:

 <clip android:clipOrientation="vertical" 定義滾動的方向 vertical為垂直方向 android:drawable="@drawable/progress1" 定義進度的圖片 android:gravity="bottom" > 定義進度的開始位置 </clip> 

5. 定義自己的樣式檔 progress_vertical.xml

 <?xmlversion="1.0"encoding="utf-8"?> <layer-listxmlns:android="https://schemas.android.com/apk/res/android"> <itemandroid:id="@android:id/progress"> <clip android:clipOrientation="vertical" android:drawable="@drawable/progress1" android:gravity="bottom"> </clip> </item> </layer-list>

6. 套用自訂樣式

 <Button android:id="@+id/btStart" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="150dp" android:text="開始"/> <ProgressBar android:id="@+id/pbPic" android:layout_width="50dp" android:layout_height="68dp" android:background="@drawable/progress2" android:max="100" android:progress="0" android:progressDrawable="@drawable/progress_vertical" /> <!-- 在此屬性上應用 --> <TextView android:id="@+id/txtProgress" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

7. 用按鈕模擬進度推進

 publicclass ProgressActivity extends Activity { ProgressBar pb = null; TextView txtProgress; Handler handler = new Handler(); @Override publicvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); System.out.println("主題=" + getTheme() + ""); pb = (ProgressBar) findViewById(R.id.pbPic); Button btnStart = (Button) findViewById(R.id.btStart);//按鈕 txtProgress = (TextView) findViewById(R.id.txtProgress);//顯示進度 btnStart.setOnClickListener(new OnClickListener() {//按鈕點擊事件 publicvoid onClick(View v) { new Thread(new Runnable() {//創建並啟動線程,使用線程執行模擬的任務 publicvoid run() { for (inti = 0; i < 100; i++) { //循環100遍 try { handler.post(new Runnable() { //更新界面的數據 publicvoid run() { pb.incrementProgressBy(1);//增加進度 txtProgress.setText(pb.getProgress() + "%");//顯示完成的進度 } }); Thread.sleep(100); } catch (InterruptedException e) { } } } }).start(); } }); } }

這裡刻意把「計算」與「繪製」分開:耗時的推進放在子執行緒,UI 更新則透過 Handler 拋回主執行緒,這是 Android 更新介面的標準做法,直接在工作執行緒改 ProgressBar 會出錯。

以上就是自訂進度條的完整流程。整段做法最值得帶走的是那個「讀源碼」的習慣——系統元件的外觀幾乎都能靠一層 layer-list 重現或改寫,先看官方怎麼做,再決定自己怎麼改,往往比從零摸索快得多。

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