編輯:關於 Android 程式設計
專案裡若大量使用圖片按鈕,還逐一準備 pressed 狀態的圖會很麻煩。這裡的做法是重寫 ImageButton,在按下時用 ColorMatrix 把圖片降飽和成灰階,放開後還原,一套素材就能應付所有按鈕。
package me.henji.widget;
import android.content.Context;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
/**
* 自定義圖片按鈕(ImageButton),按下顏色改變
* @author Leo
* @created 2013-3-15
*/
public class CmButton extends ImageButton implements OnTouchListener, OnFocusChangeListener {
public CmButton(Context context) {
super(context);
this.setOnTouchListener(this);
this.setOnFocusChangeListener(this);
}
public CmButton(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.imageButtonStyle);
this.setOnTouchListener(this);
this.setOnFocusChangeListener(this);
}
public CmButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFocusable(true);
this.setOnTouchListener(this);
this.setOnFocusChangeListener(this);
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
// 灰色效果
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
if (hasFocus) {
((ImageButton) v).getDrawable().setColorFilter(new ColorMatrixColorFilter(cm));
} else {
((ImageButton) v).getDrawable().clearColorFilter();
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// 灰色效果
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
((ImageButton) v).getDrawable().setColorFilter(new ColorMatrixColorFilter(cm));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
((ImageButton) v).getDrawable().clearColorFilter();
}
return false;
}
}
<me.henji.widget.CmButton
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:src="@drawable/button_login"
android:text="@string/login_login" />
ColorMatrix 是一個 4×5 的矩陣,用來對每個像素的 RGBA 值做線性變換。setSaturation(0) 把飽和度設為 0,等價於套用亮度作為灰階值,視覺效果就是「按下去變灰」。設為 1 則是完全不變。這比準備兩套圖片省下大量素材與 APK 體積。
自定義 View 必須實作三個建構子,因為 Android 會依使用情境選擇呼叫哪一個:
new CmButton(context) 時呼叫第一個漏掉任何一個,在該情境下就會出現 inflate 失敗的例外。
這裡 onTouch() 回傳 false,表示不消費事件,讓事件繼續往下傳遞給 OnClickListener。這麼做是對的——若回傳 true,點擊事件會被吃掉,setOnClickListener() 就永遠不會觸發。但更穩健的寫法是呼叫 super.onTouchEvent(event) 並回傳其結果,能保留 ImageButton 原有的行為。
目前只處理 ACTION_DOWN 與 ACTION_UP。使用者按下按鈕後手指滑出範圍再放開,會觸發 ACTION_CANCEL 而非 UP,此時 colorFilter 不會被清除,按鈕會卡在灰色狀態。應該補上:
else if (event.getAction() == MotionEvent.ACTION_UP
|| event.getAction() == MotionEvent.ACTION_CANCEL) {
((ImageButton) v).getDrawable().clearColorFilter();
}
每次 onTouch 都 new 一個 ColorMatrix 與 ColorMatrixColorFilter,在頻繁觸碰時會造成不必要的 GC。改成類別層級的 static final 成員即可。
直接對 getDrawable() 設 filter,會改動到該 Drawable 的所有使用者。若同一個 drawable 資源被多個按鈕共用,一個按下去可能導致其他按鈕一起變色。這在預設情況下不會發生(因為 Drawable 已 implicitly mutate),但顯式呼叫 drawable.mutate() 才是保證正確的做法。
如果你的需求只是「按下換圖」,其實不需要自定義 View。用 ImageButton 搭配 selector 即可:
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_login_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/button_login" />
</selector>
自定義 CmButton 的價值在於「不想準備第二張圖」——用程式化方式產生的視覺回饋,這才是 ColorMatrix 方案的真正優勢所在。
Android 基礎入門教程——三個繪圖工具類詳解
前兩小節我們學過 Drawable 與 Bitmap,都是負責「加載」圖片的。本節要介紹的則是繪圖相關的三個核心 API:Canvas(畫布)、Paint(畫筆)、Pat
從四個方法理清 Android 觸摸事件的分發與消費機制
提到 Android 的觸摸事件處理,很多人第一反應就是那幾個「令人頭疼」的方法: @Override public boolean onTouchEvent(Moti
手機真機調試環境搭建:用 Fiddler 做代理調試微信與 Android、iOS 開發
最近在推進一個微信相關的項目。調試內網環境下的微信公眾號開發時,需要在手機上訪問只在本地 hosts 裡生效的觸屏版頁面,改來改去非常折騰。折騰一陣之後,我找到了 Fid
從 Eclipse 轉戰 Android Studio:一次升級路上的踩坑記錄
Android Studio 剛問世時我就裝來玩過。那時候介面單薄,功能密度遠不如後來的 1.2、1.3,加上建置一個專案得等上老半天、對網路的依賴又重,試了一陣子就放回