編輯:關於 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 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>
這樣做雖然通用性佳,卻有點麻煩——每個按鈕都得準備多張圖片,甚至再加一份版面配置。如果一個遊戲裡有幾百個按鈕,那還得了?於是有了下面這個更省事的做法。
/**
* 按下按鈕時的顏色過濾矩陣
*/
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,抬起時再還原。這樣一來,不論有多少顆按鈕,都能共用同一套效果,不必為每顆按鈕準備一堆圖片,維護起來輕鬆許多。
Android Handler 多執行緒入門:用 Looper 串接計數與 UI 更新
從主線程到自訂執行緒在前一篇範例中,真正負責更新介面的仍然是主線程,也就是 UI 線程。若想改用自行建立的執行緒來完成計數,再透過 Handler 把結果送回主線程,就能
高仿微信圖片選擇器:Android 海量圖片的掃描、加載與流暢體驗
一、為什麼要認真對待圖片加載 手機鏡頭的像素輕鬆破千萬,一張照片解釋成 Bitmap 之後所佔的內存往往相當可觀。如果直接把幾千張原圖一股腦塞進列表,閃退與卡頓幾乎是必然
RecyclerView 完全解析(三十):結合 AndroidAnnotations 注入框架寫一個列表
一、前言 RecyclerView 面市已經不短時間,口碑一直不錯。相比 ListView、GridView,它在數據綁定、Item View 創建、回收複用等機制上都有
用 Lambda 表達式取代 Android 中的匿名內部類
Lambda 是希臘字母表的第十一碼(大寫 Λ、小寫 λ)——嗯,跑題了。回到正題:Lambda 表達式是 Java 8 的招牌新特性之一,同批登場的還有: Lambd