Android ImageButton Click the toggle button picture effect

Source: Internet
Author: User

The effect of the normal state:

Button-pressed slices:

First, in Java for the Picture button to increase the touch monitoring function to achieve the picture switch, the code is as follows:

ImageButton BTN =(ImageButton) Findviewbyid (R.id.imagebutton1); Btn.setontouchlistener (NewView.ontouchlistener () { PublicBoolean OnTouch (View V, motioneventEvent) {                                    if(Event. getaction () = =Motionevent.action_down) {                            //Reset the background picture when pressed( (ImageButton) v). Setimagedrawable (Getresources (). getdrawable (r.drawable.android_btn_pressed)); }Else if(Event. getaction () = =motionevent.action_up) {                             //change to normal picture when lifted( (ImageButton) v). Setimagedrawable (Getresources (). getdrawable (R.DRAWABLE.ANDROID_BTN)); }                    return false; }             });

The code is relatively simple, that is, when the picture is pressed, modify the background image of the button, when lifted up and then modified to the normal picture display.

Second, by giving the button configuration XML file to achieve the background switch effect of the picture button, the method is as follows:

1) Add a image_btn_press.xml file under layout with the following content:

<?xml version="1.0"encoding="Utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false"android:drawable="@drawable/android_btn"/> <item android:state_focused="true"android:drawable="@drawable/android_btn"/> <item android:state_pressed="true"android:drawable="@drawable/android_btn_pressed"/> </selector>

To set the properties of a picture button in Main.xml:

 <imagebutton android:id  = " 
   
    @+id/imagebutton2  
      Android:layout_ Width  = wrap_content   "  android:layout_height  ="  Span style= "color: #800000;" >wrap_content    Android:background  =  @layout/image_btn_press  Span style= "color: #800000;" > "/> 
   

Specify the property value of background in Imagebuttonk: @layout/image_btn_press, where image_btn_press is the XML configuration file created above for the picture ammonium button. At the same time, you can also put the Image_btn_press.xml file in the drawable directory, at this time, it is necessary to modify its property value to: @drawable/image_btn_press can, that is, the correct path to specify its XML file is OK.

In addition, you need to pay special attention to: in ImageButton, if you use an XML configuration file to set the effect of the picture, do not specify its android:src= "" attribute value, or the image will not be the effect of the press.

Android ImageButton Click the toggle button picture effect

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.