Imagebutton inherits from imageview. As its name suggests, it uses an image as a button for clicking.
1. Main attributes
(1) You can use its Android: src attribute or setimageresource (INT sourceid) method to specify the image to be used.
(2) You can use setalpha (INT alpha) to set the transparency of the imagebutton image (not the background image ). Alpha transparency value 0 ~ 255, 0 is completely transparent, and is completely opaque.
2. Set display in different States
To indicate different button states, we usually use selector to define different images in various states for imagebutton. The following is an example of an android API:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/button_focused" /> <!-- focused --> <item android:drawable="@drawable/button_normal" /> <!-- default --></selector>
Save the preceding XML content to the res/drawable/folder, name it image_button_selector.xml, and set the file name as a parameter "@ drawable/image_button_selector" to the Android: src attribute of imagebutton.
Note:
(1) state_pressed and state_focused indicate the status when imagebutton is pressed and the focus is obtained.
(2) The order of item elements is very important, because it is to determine whether it is applicable to the current button status based on this order, which is why the images in the normal (default) status are placed at the end, it will only be used after both pressed and focused fail to judge.
* Application Skills
1. Effect of pressing the image button when it is clicked
You can set the image transparency to display the click effect.
Imagebutton. setontouchlistener (new view. ontouchlistener () {public Boolean ontouch (view V, motionevent event) {If (event. getaction () = motionevent. action_down) {imagebutton. getdrawable (). setalpha (150); imagebutton. invalidate (); // redraw button} else {imagebutton. getdrawable (). setalpha (255) imagebutton. invalidate () ;}return false ;}}