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

Android 自定義 ProgressBar:用 View 寬度變化實作進度條

編輯:關於 Android 程式設計

系統內建的 ProgressBar 樣式有限,若想做出完全貼合設計稿的橫向進度條,最簡單的切入點是「自訂一個容器,讓內部 View 的寬度隨進度改變」。這個做法不需要自繪 Canvas,也不必處理 NinePatch,程式碼量極小。

一、版面配置

外層是自訂的 ProgressView(繼承 RelativeLayout),內含一個真正顯示顏色的 View。注意自訂 View 的標籤必須寫成「完整套件名稱.類別名稱」,與 java 檔案的 package 一致,否則 LayoutInflater 找不到類別:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <com.jltxgcy.progressbar.ProgressView
        android:id="@+id/rootview"
        android:layout_width="match_parent"
        android:layout_height="15dp"
        android:background="#d6d6d6" >

        <View
            android:id="@+id/progress"
            android:layout_width="match_parent"
            android:layout_height="15dp" />

    </com.jltxgcy.progressbar.ProgressView>

</RelativeLayout>

二、自訂 ProgressView

兩個關鍵點:第一,透過 onFinishInflate 取得內部子 View 的參照,這是自訂 ViewGroup 找到子元素的標準時機;第二,setProgress 用 postDelayed 延遲 20 毫秒執行,因為在這個時間點之前 View 尚未完成測量,getWidth() 會回傳 0,算出來的寬度自然全錯。

package com.jltxgcy.progressbar;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

public class ProgressView extends RelativeLayout {

    private View mProgress;

    public ProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mProgress = findViewById(R.id.progress);
    }

    public void setBackgroud() {
        mProgress.setBackgroundColor(Color.BLUE);
    }

    public void setProgress(final int progress) {
        postDelayed(new Runnable() {
            @Override
            public void run() {
                int width = getWidth();
                int height = getHeight();
                int curProgress = (int) (progress / 100.0 * width);
                LayoutParams params = new LayoutParams(curProgress, height);
                mProgress.setLayoutParams(params);
            }
        }, 20);
    }
}

用 LayoutParams 重新指定寬度之所以會生效,是因為 RelativeLayout 會在 requestLayout 之後重新為子 View 配置位置。若換成 LinearLayout 搭配 weight,寫法會更乾淨,也不必延遲取值。

三、在 Activity 中驅動

透過 findViewById 取得自訂 View,再用 postDelayed 建立一個每 500 毫秒遞增的迴圈:

package com.jltxgcy.progressbar;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    private ProgressView mProgressView;
    private int progressRate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mProgressView = (ProgressView) findViewById(R.id.rootview);
        mProgressView.postDelayed(new UpdateProgressBar(), 500);
    }

    class UpdateProgressBar implements Runnable {
        @Override
        public void run() {
            mProgressView.setBackgroud();
            mProgressView.setProgress(progressRate);
            progressRate += 2;
            mProgressView.postDelayed(new UpdateProgressBar(), 500);
        }
    }
}

可以更好的地方

這個實作的進度上限沒有做檢查,progressRate 持續遞增會讓寬度超過父容器。實務上應在 setProgress 裡夾住範圍(例如 Math.max(0, Math.min(100, progress)))。另外,若只是要顯示進度而非自訂外觀,直接繼承 ProgressBar 並覆寫 onDraw,或使用 ClipDrawable 搭配 progressDrawable,都能少寫不少程式碼。

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