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

為 ImageButton 自訂按下效果的三種做法

編輯:關於 Android 程式設計

使用 Button 或 ImageButton 時,若希望使用者點擊時有「按下」的回饋感,常見有兩種做法:在程式碼中監聽觸碰事件,或用 XML 的 selector 定義狀態。本文除了這兩種,再分享一個用 ColorFilter 一次搞定大量按鈕的巧思。

做法一:在程式碼中監聽觸碰

imageButton.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // 按下時換成按下狀態的背景圖
            v.setBackgroundResource(R.drawable.pressed);
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            // 抬起時還原背景圖
            v.setBackgroundResource(R.drawable.released);
        }
        return false;
    }
});

做法二:用 XML selector 定義狀態

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="false"
        android:drawable="@drawable/button_add" />
    <item
        android:state_pressed="true"
        android:drawable="@drawable/button_add_pressed" />
    <item
        android:state_focused="true"
        android:drawable="@drawable/button_add_pressed" />
    <item
        android:drawable="@drawable/button_add" />
</selector>

把這個檔案放在 drawable 目錄下(例如命名為 button_add_x.xml),使用時指定給 src 即可:

<ImageButton
    android:id="@+id/ImageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#00000000"
    android:src="@drawable/button_add_x" >
</ImageButton>

這樣做雖然通用性佳,卻有點麻煩——每個按鈕都得準備多張圖片,甚至再加一份版面配置。如果一個遊戲裡有幾百個按鈕,那還得了?於是有了下面這個更省事的做法。

做法三:用 ColorFilter 做顏色偏移

/**
 * 按下按鈕時的顏色過濾矩陣
 */
public final static float[] BT_SELECTED = new float[] {
    2, 0, 0, 0, 2,
    0, 2, 0, 0, 2,
    0, 0, 2, 0, 2,
    0, 0, 0, 1, 0 };

/**
 * 按鈕恢復原狀的顏色過濾矩陣
 */
public final static float[] BT_NOT_SELECTED = new float[] {
    1, 0, 0, 0, 0,
    0, 1, 0, 0, 0,
    0, 0, 1, 0, 0,
    0, 0, 0, 1, 0 };

/**
 * 按鈕焦點改變
 */
public final static OnFocusChangeListener buttonOnFocusChangeListener = new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));
            v.setBackgroundDrawable(v.getBackground());
        } else {
            v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));
            v.setBackgroundDrawable(v.getBackground());
        }
    }
};

/**
 * 按鈕觸碰按下效果
 */
public final static OnTouchListener buttonOnTouchListener = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));
            v.setBackgroundDrawable(v.getBackground());
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));
            v.setBackgroundDrawable(v.getBackground());
        }
        return false;
    }
};

/**
 * 為圖片按鈕設定焦點與觸碰狀態
 */
public final static void setButtonFocusChanged(View inView) {
    inView.setOnTouchListener(buttonOnTouchListener);
    inView.setOnFocusChangeListener(buttonOnFocusChangeListener);
}

使用時只要呼叫 setButtonFocusChanged(view) 即可。

原理:利用 Drawable 的 setColorFilter() 對圖片做顏色偏移過濾,按下時套用一組讓畫面變亮(或變暗)的 ColorMatrix,抬起時再還原。這樣一來,不論有多少顆按鈕,都能共用同一套效果,不必為每顆按鈕準備一堆圖片,維護起來輕鬆許多。

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