方法3、监听TouchEvent ,使用ColorFilter :
定义两个数组:
public final float[] BTN_PRESSED = new float[] {
2, 0, 0, 0, 2,
0, 2, 0, 0, 2,
0, 0, 2, 0, 2,
0, 0, 0, 1, 0,
};
public final float[] BTN_NOPRESSED = new float[] {
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0,
};
//在点击事件时处理
private OnTouchListener mOnTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (MotionEvent.ACTION_DOWN == event.getAction()) {
v.getBackground().setColorFilter(
new ColorMatrixColorFilter(BTN_PRESSED ));
v.setBackgroundDrawable(v.getBackground());
} else {
v.getBackground().setColorFilter(
new ColorMatrixColorFilter(BTN_NOPRESSED ));
v.setBackgroundDrawable(v.getBackground());
}
return false;
}
};
btn.setOnTouchListener(mOnTouchListener);