The ImageButton class is rewritten because the project has more of this type of picture buttons.
Copy Code code as follows:
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;
/**
* Custom Picture button (ImageButton), press the color change
* @author Leo
* @created 2013-3-15
*/
public class Cmbutton extends ImageButton implements Ontouchlistener, Onfocuschangelistener {
Public Cmbutton {
Super (context);
This.setontouchlistener (this);
This.setonfocuschangelistener (this);
}
Public Cmbutton (context context, AttributeSet Attrs) {
This is the 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) {
//Gray effect
colormatrix cm = new ColorMatrix ();
cm.setsaturation (0);
if (hasfocus) {
((ImageButton) v). Getdrawable (). Setcolorfilter (New Colormatrixcolorfilter (cm));
} else {
((ImageButton) v). Getdrawable (). Clearcolorfilter ();
 &NBSP}
}
@Override
public boolean Ontouch (View V, motionevent event) {
Grey effect
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;
}
}
Layout file
Copy Code code as follows:
<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"/>