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

RingProgressBar:簡潔的圓環進度條控件

編輯:關於 Android 程式設計

專案地址:RingProgressBar

一個自製的圓環進度條,很適合用在檔案上傳/下載、圖片載入等需要表達百分比的場合,樣式走 Material Design 風格。

效果預覽

引入方式

Step 1. 在根目錄的 build.gradle 加入 JitPack 倉庫:

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

Step 2. 加入依賴:

dependencies {
    compile 'com.github.HotBitmapGG:RingProgressBar:V1.2.1'
}

自訂屬性

屬性名格式說明
ringColorcolor圓環底色
ringProgressColorcolor進度部分的顏色
ringWidthdimension圓環線寬
textColorcolor文字顏色
textSizedimension文字大小
maxinteger進度上限
textIsShowboolean是否顯示文字
styleSTROKE / FILL圓環樣式(描邊或填充)

使用方式

1. 在版面配置中宣告

<io.netopen.hotbitmapgg.library.view.RingProgressBar
    android:id="@+id/progress_bar_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    android:layout_centerInParent="true"
    app:max="100"
    app:ringColor="@color/colorPrimary"
    app:ringProgressColor="@color/colorPrimaryDark"
    app:ringWidth="3dp"
    app:textColor="@color/colorPrimary"
    app:textIsShow="true"
    app:textSize="16sp" />

<io.netopen.hotbitmapgg.library.view.RingProgressBar
    android:id="@+id/progress_bar_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="100dp"
    android:layout_alignParentBottom="true"
    android:layout_centerInParent="true"
    app:max="100"
    app:ringColor="@android:color/darker_gray"
    app:ringProgressColor="@color/colorPrimary"
    app:ringWidth="3dp"
    app:textColor="@color/colorPrimary"
    app:textIsShow="true"
    app:textSize="16sp" />

2. 在程式碼中驅動

mRingProgressBar = (RingProgressBar) findViewById(R.id.progress_bar);

// 設定進度
mRingProgressBar.setProgress(progress);

// 進度到達上限時的回呼(預設最大值為 100)
mRingProgressBar.setOnProgressListener(new RingProgressBar.OnProgressListener() {
    @Override
    public void progressToComplete() {
        Toast.makeText(MainActivity.this, "complete", Toast.LENGTH_SHORT).show();
    }
});

小提醒

進度滿之後原始碼裡還有一段「再累加一格以觸發動畫」的處理,作者認為實用性不高而註解掉了。若有需要,自行取消註解即可。

作者的相關作品

  • 知了日報客戶端:https://github.com/HotBitmapGG/RxZhiHu
  • 高仿 BiliBili 客戶端:https://github.com/HotBitmapGG/OhMyBiliBili
  • Gank.io 客戶端:https://github.com/HotBitmapGG/StudyProject
  • 圓環進度條:https://github.com/HotBitmapGG/RingProgressBar
  • 仿芝麻信用分儀表盤:https://github.com/HotBitmapGG/CreditSesameRingView
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved