Basic Components of Android 05-imagebutton

Source: Internet
Author: User

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 ;}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.