Set the Android ImageView style when clicked

Source: Internet
Author: User
Tags touch


Imageview.setxxx can set
Setbackgroundresource background,
Layout Setlayoutparams
and Setscaletype,
The source picture cannot be changed. Don't worry. If the picture is rotated, only another bitmap is generated

Solutions

First, create an XML file under the Res/drawable folder, named Logoutimage.xml, in which the image is actually clicked before and when clicked, the code is as follows:

  code is as follows copy code

<?xml Version= "1.0" encoding= "Utf-8"
<selector xmlns:android= "Http://schemas.android.com/apk/res/android"
    <!--default picture--> 
    <item android:state_pressed= "false" & nbsp
        android:drawable= "@drawable/logout2"/>
    <!--click picture--> 
    <item android:state_pressed= "true"  
         android:drawable= "@drawable/logout1"/> 
</selector>

The file is then referenced in the src attribute of ImageView, and the code is as follows:

The code is as follows Copy Code

<imagebutton
android:src= "@drawable/logoutimage"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
/>

More information

The code is as follows Copy Code

<!--default picture-->   
<item android:drawable= "@drawable/img1"/>
 
<!-- Picture-->    with no focus
<item android:state_window_focused= "false"   
       android:drawable= "@drawable/img2"/>&NBSP;&NBSP;&NBSP;&NBSP;

<!--picture--> when getting focus
<item android:state_focused= "true"
android:drawable= "@drawable/img3"/>

< pictures When selected!---->
<item android:state_selected= "true"
android:drawable= "@drawable/img4"/>

<!--picture--> when clicked in touch mode
<item android:state_focused= "false"
Android:state_pressed= "true"
android:drawable= "@drawable/img5"/>

< picture--> When you get focus and click in non-touch mode!--
<item android:state_focused= "true"
Android:state_pressed= "true"
android:drawable= "@drawable/img6"/>

Method of these, we can look at the following effect chart

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.