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

Android 自定義 View 實戰:打造可滑動的 SlideTab 選擇器

編輯:關於 Android 程式設計

接到的需求很簡單:一條橫線上等距排布 n 個節點,每個節點上方一個圓、下方一段文字;選中哪一個,圓變成藍色空心圈、文字也變藍。這種控制項在系統裡找不到現成的,正好拿來做自定義 View 的練習。

第一步:拆解元素,重寫 onDraw

先把畫面上有哪些東西列出來:一條基準線、n 個圓(未選中是實心小圓,選中是較粗的空心圓)、n 段文字。接著把需要的成員變數定下來:

· 資料:String[] tabNames 提供選項文字,int selectedIndex 記錄當前選中序號
· 樣式:mTextSize 字級、mColorTextDef 未選中文字色、mColorDef 線與圓的顏色、mColorSelected 選中色
· 尺寸:mLineHight 線粗、mCircleHight 圓直徑、mCircleSelStroke 選中空心圓筆畫寬度、mMarginTop 圓與文字的間距

兩個關鍵的計算量:lineLength 等於控件寬度減去左右各半個圓半徑,這樣線的兩端才會正好落在兩側圓心上;splitLength = lineLength / (n - 1) 則是相鄰圓之間的間距。

這裡有個容易忽略的細節:線長必須等測量完成、寬度確定之後才能算,所以這些計算要放在 onMeasure() 裡,不能寫在構造方法中。

於是流程是:繼承 View → 覆寫帶 AttributeSet 的構造方法讀取自定義屬性 → 在 onDraw() 中用 drawLine、drawCircle、drawText 把輪廓畫出來。

佈局先簡單放進去:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dip">

    <com.openxu.st.slidetab.SlideTab
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#aaff0000" />
</LinearLayout>

刻意給了半透明紅底,方便確認控件實際佔了多大範圍。

此時會發現:明明寫的是 wrap_content,紅色區塊卻填滿整個螢幕高度。這正是自定義 View 一定要處理的測量問題。

第二步:重寫 onMeasure 決定高度

這個控件的高度應該是「圓的直徑 + 文字高度 + 兩者間距」,寬度則直接沿用父容器給的結果。把自定義屬性透過 declare-styleable 開放出來:

<declare-styleable name="SlideTab">
    <attr name="textcolordef" format="color" />
    <attr name="textsize"    format="dimension" />
    <attr name="defcolor"    format="color" />
    <attr name="selectedcolor" format="color" />
    <attr name="linthight"   format="dimension" />
    <attr name="circlehight" format="dimension" />
    <attr name="circleselstroke" format="dimension" />
    <attr name="mmargintop"  format="dimension" />
    <attr name="tabnames"    format="reference" />
</declare-styleable>

佈局中的用法:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:openxu="https://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dip">

    <com.openxu.st.slidetab.SlideTab
        android:id="@+id/slideTab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textsize="15sp"
        openxu:circlehight="20dip"
        openxu:circleselstroke="5dip"
        openxu:defcolor="#EAEAEA"
        openxu:linthight="2dip"
        openxu:mmargintop="15dip"
        openxu:selectedcolor="#5CBB8C"
        openxu:tabnames="@array/tab_names"
        openxu:textcolordef="#A4A4A4" />
</LinearLayout>

文字清單放在 res/values/array.xml 的 string-array 裡,程式從 TypedArray.getTextArray() 取出後轉成字串陣列即可。

高度正常之後,剩下的就是滑動手勢與位置換算:攔截 onTouchEvent 的 ACTION_DOWN / MOVE,把觸發點的 x 座標除以 splitLength 並四捨五入,就得到應該選中的索引;索引若有變化,更新 selectedIndex 並 invalidate() 重繪。若想讓藍色圓跟手 smoothly 移動,可以在 MOVE 階段記錄浮動偏移量,在 onDraw 時把選中圓畫在手指正上方,抬起時再吸附到最近的節點。

這個例子想傳達的重點其實只有三件事:自定義屬性要透過 styleable 開放出來、wrap_content 必須自己在 onMeasure 裡算、所有依賴寬度的幾何計算都要延後到測量之後。把這三點掌握好,大部分類似的選擇器、進度條、刻度尺都能照樣寫出來。

源碼下載

https://github.com/openXu/SlidingTab

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