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

Android 多種樣式的進度條實現大全

編輯:關於 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> 標簽定義,有三個關鍵屬性:

  • android:drawable:指定被截取的源 Drawable,也可以用子標簽形式聲明。
  • android:clipOrientation:截取方向,可取 horizontal 或 vertical。
  • android:gravity:截取時的對齊方式,可選 top、bottom、right、left、center、center_vertical、center_horizontal、fill_vertical、clip_vertical、start、end 等。

使用時調用 setLevel(int level) 控制截取區域大小:level 為 0 時片段為空,為 10000 時顯示整張圖。於是只要讓程序週期性地遞增 level,圖片就會呈現「漸漸展開」的動畫——這正是上面所有示例在 Runnable 裡反復調 setLevel 的原因。

掌握了這條主線,再遇到任何形狀與方向的進度條需求,本質上都是在換 shape 的 outer shape 與 clip 的 gravity 組合而已。

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