Android中ImageButton实现“按下”效果
?
?
?
?
?
/** * 按钮被按下 */private final static float[] BUTTON_PRESSED = new float[] { 2.0f, 0, 0, 0, -50, 0, 2.0f, 0, 0, -50, 0, 0, 2.0f, 0, -50, 0, 0, 0, 5, 0 }; /** * 按钮恢复原状 */ private final static float[] BUTTON_RELEASED = new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 }; private static final OnTouchListener touchListener = new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {if(event.getAction() == MotionEvent.ACTION_DOWN) {v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_PRESSED));v.setBackgroundDrawable(v.getBackground());}else if(event.getAction() == MotionEvent.ACTION_UP) {v.getBackground().setColorFilter(new ColorMatrixColorFilter(BUTTON_RELEASED));v.setBackgroundDrawable(v.getBackground());}return false;}};public static void setButtonStateChangeListener(View v) {v.setOnTouchListener(touchListener);}?
?
?
?
?
?
1 楼 ghostcynric 2012-02-09 大哥。。第三种方法,在虚拟机上,为什么第一次按按钮时没有反应,必须要第二次按以后才会出现效果?