Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 初階開發

Android 圓形進度條顏色自訂教學

編輯:初階開發

最近因專案需要研究 ListView 捲動時的資料動態更新,其中載入過程要顯示圓形進度條,卡住的地方是顏色設定。以下把查到的資料與自己的實作結果整理出來。

版面配置

在 XML 中加入進度條控件,並透過 indeterminateDrawable 指向自訂的 drawable:

<ProgressBar android:id="@+id/loadProgressBar"
    android:indeterminateDrawable="@drawable/progressbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

progressbar.xml

indeterminateDrawable 就是決定顏色與形狀的地方,內容如下:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="https://schemas.android.com/apk/res/android"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromDegrees="0"
    android:toDegrees="360">

    <shape
        android:shape="ring"
        android:innerRadiusRatio="3"
        android:thicknessRatio="8"
        android:useLevel="false">

        <gradient
            android:type="sweep"
            android:useLevel="false"
            android:startColor="#000000"
            android:centerColor="#FFFFFF"
            android:centerY="0.50"
            android:endColor="#FFFF00" />
    </shape>
</rotate>

效果如下:


▲ 自訂顏色的圓形進度條

參數說明

  • rotate:讓整個形狀持續旋轉,fromDegrees 到 toDegrees 決定旋轉範圍,動畫會自動循環。
  • shape="ring":必須設為 ring 才會呈現環形,設成 oval 會變成實心圓。
  • innerRadiusRatio / thicknessRatio:分別控制內圈半徑與環寬的比例,數值越小環越粗。
  • gradient type="sweep":掃描式漸層,是產生「一側亮一側暗」旋轉感的關鍵;改為 linear 或 radial 視覺效果會完全不同。
  • startColor / centerColor / endColor:三個色階自訂配色,這裡示範黑→白→黃的漸層。

各參數可依設計稿自行調整。若想改成不旋轉的純色圓環,把外層 rotate 拿掉、並改用 shape 的 solid 填色即可。

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