Java code will not be pasted, only the XML file will be mounted,
Method:
1. Put three images in drawable, which are the images that obtain the focus, press the focus, lose the focus, and press the focus, and the natural state (that is, the focus is lost, and the focus is not pressed ). Here you prepare three photos.
2. Create an imagebutton. xml file in the drawable folder. The content in the file is:
<Selector xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item
Android: state_focused = "true"
Android: state_pressed = "false"
Android: drawable = "@ drawable/snake"/>
<Item
Android: state_focused = "true"
Android: state_pressed = "true"
Android: drawable = "@ drawable/Solitaire"/>
<Item
Android: state_focused = "false"
Android: state_pressed = "true"
Android: drawable = "@ drawable/Solitaire"/>
<Item
Android: drawable = "@ drawable/tunes"> </item>
</Selector>
3. reference the XML file in the layout file:
<Imagebutton
Android: Id = "@ + ID/imgbutton"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: Background = "@ drawable/image_button"/>