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

用 XML Bitmap 的 tileMode 實作波浪線,告別切圖變形

編輯:關於 Android 程式設計

先講結論:要畫一條無論多寬都不變形的虛線或波浪線,用 XML 的 bitmap 標籤加上 tileMode 平鋪,是最省事的做法。

一、先認識 XML Bitmap

XML Bitmap 是一個放在資源目錄、以 XML 描述「圖片」的檔案。它替既有的 bitmap 建立別名,並可額外指定抖動、抗鋸齒、對齊、平鋪模式等屬性。

存放位置:res/drawable/filename.xml

語法:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="https://schemas.android.com/apk/res/android"
    android:src="@[package:]drawable/drawable_resource"
    android:antialias=["true" | "false"]
    android:dither=["true" | "false"]
    android:filter=["true" | "false"]
    android:gravity=["top" | "bottom" | "left" | "right"
                   | "center_vertical" | "fill_vertical"
                   | "center_horizontal" | "fill_horizontal"
                   | "center" | "fill"
                   | "clip_vertical" | "clip_horizontal"]
    android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />

其中 tileMode 的四個值分別是:disabled(不平鋪)、clamp(邊緣像素延伸填滿)、repeat(重複平鋪)、mirror(鏡像平鋪)。

一個簡單對照:

<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"
    tools:context=".MainActivity" >

    <!-- 套用自訂的 bitmap 背景 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/bm"
        android:text="sssssssssssssssssss" />

</RelativeLayout>

效果對照

預設背景(@drawable/btn_default_pressed_holo_light):

改用 @drawable/bm 之後:

差異在截圖中相當明顯。

加上 tileMode="repeat" 的效果:

二、常見的三種波浪線做法比較

做法一:直接用一張波浪線切圖

最簡單,但缺點也很直接——寬度一大就會被拉伸變形:

寬度不足時則會被壓縮:

要求不高時勉強可用,若在意細節就完全不夠看。

做法二:自訂 View 繪製

效果確實最好,任意寬度都能保持波形一致,但實作成本相對高,需要自己處理 onMeasure 與 onDraw。

做法三:XML bitmap 平鋪

兼顧效果與成本。思路是準備一個「週期單位」——包含一個波峰與一個波谷——再讓系統在水平方向重複平鋪。

wave_item(單一週期):

wave.xml:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="https://schemas.android.com/apk/res/android"
    android:antialias="true"
    android:src="@drawable/wave_item"
    android:tileMode="repeat" />

activity_main.xml:

<LinearLayout 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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/wave" />

</LinearLayout>

三、關鍵細節

這裡最容易失敗的一點:ImageView 必須用 background 才能看到平鋪效果,改用 src 是看不出來的。原因是 tileMode 屬於背景绘制(Drawable)階段的行為,而 src 走的是 bitmap 縮放流程。

另一個實務提醒:切圖寬度若與裝置密度不匹配,平鋪後可能出現約 1px 的接縫。建議把 wave_item 準備成多組密度版本(drawable-mdpi/xhdpi 等),並確認單元寬度在整數像素下能整除,接縫問題就不會出現。

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