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

Android 自定義控件實戰:用 Shape 畫出圓形進度條

編輯:關於 Android 開發

Android 自定義控件(四):Shape 也能畫出圓形進度條

系統內建的 ProgressBar 預設是橫向長條,若想要一個轉個不停的環形載入動畫,其實不必急著寫自定義 View。把 shape 標籤寫成 ring,再外包一層 rotate,接上一個 style,就能得到乾淨俐落的圓形進度條。

一、用 rotate 包住 ring 型 shape

在 drawable 目錄新增一個 XML 資源檔(New → Drawable resource file),把預設的根標籤 selector 改成 shape,再用 rotate 在外層包一層,讓漸層顏色沿著圓周掃過:

<?xml version="1.0" encoding="utf-8"?>
<!-- 把 shape 嵌套在 rotate 裡面,實現環形旋轉 -->
<rotate xmlns:android="https://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="1080.0">

    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="9"
        android:useLevel="false">
        <gradient
            android:startColor="#ffffff"
            android:endColor="#4ec5ff"
            android:type="sweep" />
    </shape>
</rotate>

兩個關鍵點:gradient 的 type 設成 sweep(掃描式漸層),顏色才會繞著圓心鋪開;rotate 的 fromDegrees 與 toDegrees 差距越大,動畫跑道越長,這裡寫到 1080 就是讓它多繞幾圈再停。

二、把樣式收進 style 標籤

<style name="ring">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:indeterminateDrawable">@drawable/buttonringstyle</item>
</style>

三、在布局檔中掛上 ProgressBar

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.ly.blogtest.MainActivity">

    <ProgressBar
        style="@style/ring"
        android:id="@+id/progressBar" />

</RelativeLayout>

只要 indeterminateDrawable 指向剛剛那張環形圖,ProgressBar 就會自動轉起來,一行 Java 都不用寫。

四、android:shape 的四種取值

  • rectangle:矩形,預設值,直角矩形、圓角矩形、弧形邊都靠它。
  • oval:橢圓形,最常見的用法是畫正圓。
  • line:線形,實線與虛線皆可。
  • ring:環形,畫圓形進度條的關鍵。

五、只有 ring 才吃得到的專屬屬性

  • android:innerRadius:內環半徑。
  • android:innerRadiusRatio:浮點數,用「環寬的比例」表示內環半徑,預設 3,也就是內環半徑等於環寬除以 3;一旦設了 innerRadius,這個值會被覆蓋。
  • android:thickness:環的厚度。
  • android:thicknessRatio:浮點數,用比例表示厚度,預設 9,即厚度為環寬除以 9;同樣會被 thickness 覆蓋。
  • android:useLevel:一般寫 false,否則環形可能畫不出來;只有當它被當成 LevelListDrawable 使用時才設 true。

六、shape 底下可以放的子標籤

<shape>
    <!-- 實心 -->
    <solid android:color="#ff9d77"/>
    <!-- 漸變 -->
    <gradient
        android:startColor="#ff8c00"
        android:endColor="#FFFFFF"
        android:angle="270" />
    <!-- 描邊 -->
    <stroke
        android:width="2dp"
        android:color="#dcdcdc" />
    <!-- 圓角 -->
    <corners android:radius="2dp" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>

solid:填色,只有一個 android:color。

padding:內容與邊界之間的內間距,可用 left、right、top、bottom 分別指定。

gradient:漸層填色,分三種類型。

  • type 取值:linear(線性,預設)、radial(放射,必須同時設定 gradientRadius)、sweep(掃描)。
  • startColor、centerColor、endColor 分別對應起點、中段與終點顏色。
  • angle 只在線性漸層有效,必須是 45 的倍數,0 表示由左至右,90 表示由下往上。
  • centerX、centerY 是漸層中心的相對座標,取值 0.0 到 1.0,預設 0.5 即正中央,僅放射漸層有效。
  • gradientRadius 為漸層半徑,只有 radial 類型才需要。
  • useLevel 設 true 時可在 LevelListDrawable 中使用。

corners:圓角,只對 rectangle 有效。radius 是全域設定,會被下面四個單角設定覆寫:topLeftRadius、topRightRadius、bottomLeftRadius、bottomRightRadius。半徑開大(例如 200dp)就會變成弧形邊。

stroke:描邊。color 是顏色,width 是寬度,dashWidth 與 dashGap 則用來畫虛線——前者是實線段長度,後者是段與段之間的間距。

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