If imagebutton is used in Android,ProgramWhen you press the imagebutton file, you cannot see any effect.
There are two classic solutions on the Internet, one is to use XML, the other is to writeCode.
Here I would like to introduce another method to enable imagebutton to have the special effect of pressing. You only need to prepare a normal image without pressing the image.
Let's look at the sample code, create the touchlight and touchdark ontouchlistener, and set ontouchlistener for imagebutton. If touchlight is used, press the button to turn on. The other is to darken.
Import android. app. activity; import android. graphics. colormatrixcolorfilter; import android. view. motionevent; import android. view. view; import android. view. view. ontouchlistener; public class extends activity {public static final ontouchlistener touchlight = new ontouchlistener () {public final float [] bt_selected = new float [] {, 50,, 50, 0,}; public final float [] bt_not_selected = new float [] {, 0, 0, 0, 0, 1, 0}; @ overridepublic 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 static final ontouchlistener touchdark = new ontouchlistener () {public final float [] bt_selected = new float, -50,0, 0,-50,0, 0,-50,0,}; public final float [] bt_not_selected = new float, ,}; @ overridepublic 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 ;};@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); imagebutton ib1, ib2; ib1 = (imagebutton) findviewbyid (R. id. imagebutton01); ib2 = (imagebutton) findviewbyid (R. id. imagebutton02); ib1.setontouchlistener (touchlight); ib2.setontouchlistener (touchdark );}}
The two float arrays in the Code store a color matrix. It doesn't matter if you don't know the color matrix. Just use this attachment and adjust the brightness, contrast, and other values, then copy the produced color matrix to the code.
Attachment: colormatrixdemo.swf