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

自定義 Android 流程進度圖控件的完整實作

編輯:關於 Android 程式設計

先看效果:

這個控件可設定總流程數、已完成進度數、已完成部分的顏色,以及各節點的標題文字。

使用方法

1. 引入 CircleView 依賴(本控件基於它繪製圓形節點):compile 'com.github.pavlospt:circleview:1.3'

2. 把 ProcessImg.java 與 process_img.xml 複製進專案。

3. 在 XML 中聲明 ProcessImg 控件。

4. 在 Java 中取得實例並呼叫設定方法。

對外暴露的方法有三個:

setColor(int color):設定已完成進度的顏色,傳入 color 整數值。

setProcess(int total, int process):設定總流程數與已完成數。第一個參數為總數(建議 1–6,超過 6 個擠在一行視覺上會很擁擠),第二個為已完成數量。

setTitle(int position, String text):設定各節點標題。第一個參數為節點序號(1~total),第二個為標題文字。

版面檔

process_img.xml 由六組結構相同的 LinearLayout 組成,每組包含左右兩段連接線、一個 CircleView 節點與一個標題 TextView。連接線用 View 模擬,寬度以 layout_weight 分配:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    android:id="@+id/processImg_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="20dp">

    <LinearLayout
        android:id="@+id/process1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:visibility="gone">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">
            <View android:id="@+id/view1l"
                android:layout_width="0dp" android:layout_height="5dp"
                android:layout_weight="2" android:background="#00000000"/>
            <com.github.pavlospt.CircleView
                android:id="@+id/circle1"
                android:layout_width="0dp" android:layout_height="wrap_content"
                android:layout_weight="3"
                app:cv_titleText="1" app:cv_titleSize="20sp" app:cv_titleColor="#fff" />
            <View android:id="@+id/view1r"
                android:layout_width="0dp" android:layout_height="5dp"
                android:layout_weight="2" android:background="#00000000"/>
        </LinearLayout>

        <TextView android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center" android:text="text1" />
    </LinearLayout>
    <!-- process2 ~ process6 結構相同,此處省略 -->
</LinearLayout>

Java 實作

public class ProcessImg extends LinearLayout {
    private List<LinearLayout> layouts = new ArrayList<>();  // 控制流程個數
    private List<View> views = new ArrayList<>();            // 連接線
    private List<TextView> texts = new ArrayList<>();        // 標題
    private List<CircleView> circleViews = new ArrayList<>();// 節點圓
    private Context context;
    private int color = Color.parseColor("#E0E0E0");
    private int total = 0, process = 0;

    public ProcessImg(Context context) { super(context); this.context = context; initViews(); }
    public ProcessImg(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.context = context;
        initViews();
    }

    private void initViews() {
        LayoutInflater.from(context).inflate(R.layout.process_img, this);
        // 依序取得六組控制元件並放入對應 List
        circleViews.add(findViewById(R.id.circle1));
        circleViews.add(findViewById(R.id.circle2));
        circleViews.add(findViewById(R.id.circle3));
        circleViews.add(findViewById(R.id.circle4));
        circleViews.add(findViewById(R.id.circle5));
        circleViews.add(findViewById(R.id.circle6));

        layouts.add(findViewById(R.id.process1));
        layouts.add(findViewById(R.id.process2));
        layouts.add(findViewById(R.id.process3));
        layouts.add(findViewById(R.id.process4));
        layouts.add(findViewById(R.id.process5));
        layouts.add(findViewById(R.id.process6));

        texts.add(findViewById(R.id.text1));
        texts.add(findViewById(R.id.text2));
        texts.add(findViewById(R.id.text3));
        texts.add(findViewById(R.id.text4));
        texts.add(findViewById(R.id.text5));
        texts.add(findViewById(R.id.text6));

        views.add(findViewById(R.id.view1l)); views.add(findViewById(R.id.view1r));
        views.add(findViewById(R.id.view2l)); views.add(findViewById(R.id.view2r));
        views.add(findViewById(R.id.view3l)); views.add(findViewById(R.id.view3r));
        views.add(findViewById(R.id.view4l)); views.add(findViewById(R.id.view4r));
        views.add(findViewById(R.id.view5l)); views.add(findViewById(R.id.view5r));
        views.add(findViewById(R.id.view6l)); views.add(findViewById(R.id.view6r));
    }

    /** 設定已完成流程的顏色 */
    public void setColor(int color) {
        this.color = color;
        setProcess(total, process);
    }

    /** 設定進度 */
    public void setProcess(int total, int process) {
        this.total = total;
        this.process = process;

        // 依總數顯示對應數量的節點
        for (int i = 0; i < total; i++) {
            layouts.get(i).setVisibility(VISIBLE);
        }

        // 已完成節點用指定顏色,未完成用灰色
        for (int i = 0; i < circleViews.size(); i++) {
            if (i < process) {
                circleViews.get(i).setFillColor(color);
                circleViews.get(i).setBackgroundColor(color);
                circleViews.get(i).setStrokeColor(color);
                texts.get(i).setTextColor(color);
            } else {
                circleViews.get(i).setFillColor(Color.parseColor("#CCCCCC"));
                circleViews.get(i).setBackgroundColor(Color.parseColor("#CCCCCC"));
                circleViews.get(i).setStrokeColor(Color.parseColor("#CCCCCC"));
                texts.get(i).setTextColor(Color.parseColor("#CCCCCC"));
            }
        }

        // 連接線依進度上色
        for (int i = 0; i < views.size(); i++) {
            views.get(i).setBackgroundColor(i < process * 2
                    ? color : Color.parseColor("#CCCCCC"));
        }
        // 首節點左側與末節點右側不顯示連接線
        views.get(0).setBackgroundColor(Color.parseColor("#00000000"));
        if (total != 0) {
            views.get(2 * total - 1).setBackgroundColor(Color.parseColor("#00000000"));
        }
    }

    /** 設定各節點標題 */
    public void setTitle(int position, String text) {
        texts.get(position - 1).setText(text);
    }
}

設計上的幾點說明

把六組控制元件收進 List 之後,setProcess 只需要用迴圈上色,不必寫六段重複程式碼,這是這個控件最關鍵的重構。另外要留意 setProcess 中把首節點左側與末節點右側的連接線設為透明,這一步不能省,否則整條進度條兩端會多出突兀的短線。

目前的實作把節點數硬編碼為六,若要支援任意數量,建議改為在 XML 中用單一項目版面搭配 LayoutInflater 動態 inflate,再依 total 決定產生幾份,可維護性會好很多。

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