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

Android 漸層配色實戰:XML 宣告與三種 Shader 程式化繪製

編輯:關於 Android 程式設計

厭倦了單調的純色背景嗎?Android 提供了兩條路徑來做漸層:一條是在 drawable 目錄寫個 XML,幾行搞定;另一條是走 android.graphics 底下那組 Shader 類,用程式碼精細控制。兩種都整理在這裡。

路徑一:用 XML 宣告漸層

在 res/drawable 底下新增一個 XML 檔:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:angle="270"
        android:startColor="#ffffff"
        android:endColor="#000000" />

    <corners
        android:bottomLeftRadius="5dip"
        android:bottomRightRadius="5dip"
        android:topLeftRadius="5dip"
        android:topRightRadius="5dip" />
</shape>

幾個節點的職責:

  • shape:決定圖形輪廓,常見有 rectangle、oval、line、ring。
  • gradient:設定起點色、終點色與中間色,以及漸層方向。angle 為 0 是由左到右,90 是由下到上,270 是由上到下。
  • padding:控制內容與邊界的間距。
  • corners:四個圓角的半徑。

寫好之後,無論在 XML 佈局的 background 屬性還是程式碼的 setBackgroundResource() 中都能直接引用。只要你知道 RGB 值,配色就能玩出花樣。

路徑二:程式化繪製

在 android.graphics 中,與漸層相關的類都繼承自 Shader,共三兄弟:LinearGradient(線性)、RadialGradient(徑向)、SweepGradient(角度/扇形)。使用時透過 Paint 的 setShader() 掛上去即可。

LinearGradient 線性漸層

提供兩種建構方式,差別在於精度:

LinearGradient(float x0, float y0, float x1, float y1,
               int[] colors, float[] positions, Shader.TileMode tile)

LinearGradient(float x0, float y0, float x1, float y1,
               int color0, int color1, Shader.TileMode tile)

第一種可傳入顏色陣列與對應位置,做出多段細膩過渡——例如塞 20 種顏色進 colors,系統會逐段插值。第二種只吃起始色與結束色,夠用但比較樸素。

Paint p = new Paint();
LinearGradient lg = new LinearGradient(0, 0, 100, 100,
        Color.RED, Color.BLUE, Shader.TileMode.MIRROR);
p.setShader(lg);
canvas.drawCircle(0, 0, 200, p);

前四個參數是漸層的起點與終點座標,最後的 TileMode 決定超出漸層範圍後如何鋪排,MIRROR 為鏡像重複,另有 CLAMP(邊緣色延伸)與 REPEAT(循環重複)。

RadialGradient 徑向漸層

與線性版唯一的差別,是第三個參數換成了半徑:

RadialGradient(float x, float y, float radius,
               int[] colors, float[] positions, Shader.TileMode tile)

RadialGradient(float x, float y, float radius,
               int color0, int color1, Shader.TileMode tile)

前兩個參數是圓心,radius 決定漸層向外擴散多遠,超過半徑的區域就交給 TileMode 處理。

SweepGradient 角度漸層

SweepGradient(float cx, float cy, int[] colors, float[] positions)

以圓心為中心,顏色沿圓周掃過一圈。想做圓錐形或 3D 光暈效果時特別好用。官方對 positions 參數的說明是:

May be NULL. The relative position of each corresponding color in the colors array, beginning with 0 and ending with 1.0. If the values are not monotonic, the drawing may produce unexpected results. If positions is NULL, then the colors are automatically spaced evenly.

也就是說位置陣列必須單調遞增,否則結果會很怪。多數情況下傳 NULL 讓系統均勻分佈就好,或者直接用雙色版本:

SweepGradient(float cx, float cy, int color0, int color1)

快速實戰:幫視窗換個漸層背景

如果不想自己 draw,也可以直接造一個 GradientDrawable 塞給視窗:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.login);

    GradientDrawable grad = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM,
            new int[]{Color.BLACK, Color.WHITE});
    getWindow().setBackgroundDrawable(grad);
}

小結一下選型:只需要一個靜態漸層背景,寫 XML 最省事;需要跟著動畫變色、或者要畫在非矩形區域上,就走 Shader + Paint 這條路。

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