Android programming Simple to achieve ImageView click on the background image modification method _android

Source: Internet
Author: User

The example of this article describes the Android programming simple implementation of ImageView click of the background image modification method. Share to everyone for your reference, specific as follows:

When using ImageView, when clicked, want the background image to be modified, so that the click effect is obvious. Here, a very simple method, at least a very clear way. Create an XML file under the Res/drawable folder. For example My.xml, the contents are as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android=
"http://schemas.android.com/apk/res/" Android "> 
  <item android:state_pressed=" true "
    android:drawable=" @*android:drawable/btn_search_ Dialog_voice_pressed "/>
  <item android:state_pressed=" false "
    android:drawable=" @*android:drawable/ Btn_search_dialog_voice_default "/>
</selector>

Here to pay attention to a point, that is, in the above drawable is a reference to the system's picture resources, if the use of their own picture resources, to use the following format

Copy Code code as follows:
android:drawable= "@drawable/ic_desk_point_normal"

Then, in the XML file that defines the ImageView, set:

android:src= "@drawable/youpicture"
android:background= "@drawable/my"

The above can complete the effect.

Remember ImageView to add clickable= "true", otherwise its selector is not effective.

Save the following XML as an. xml file (such as List_item_bg.xml), and the runtime system uses the corresponding background image according to the status of the list items in the ListView.

Drawable/list_item_bg.xml

<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android=   
"http://schemas.android.com/apk/res/" Android > 
 <!--default background picture-->  
 <item android:drawable= "@drawable/pic1"/> 
 <!-- Background picture with no focus-->  
 <item android:state_window_focused= "false"  
    android:drawable= "@drawable/pic1"/>   
 < background picture-->  
 <item android:state_focused= "true" android:state_pressed= "true" when the focus is received in non-touch mode and clicked   
    android:drawable= "@drawable/pic2"/>  
 <!--touch mode when clicked background picture-->  
 <item android:state_focused = "false" android:state_pressed= "true"   
    android:drawable= "@drawable/pic3"/>   
 <!--picture background When selected-->  
 <item android:state_selected= "true"   
    android:drawable= "@drawable/pic4"/>   
 <!--the picture background when the focus is obtained -->  
 <item android:state_focused= "true"   
    android:drawable= "@drawable/pic5"/>   
</ Selector>

How to use:

The first is to configure in ListView

Copy Code code as follows:
Android:listselector= "@drawable/LIST_ITEM_BG"

The second is to add attributes to the ListView item
Copy Code code as follows:
android:background= "@drawable/LIST_ITEM_BG"

The third type is used in Java code:

drawable drawable = Getresources (). getdrawable (R.DRAWABLE.LIST_ITEM_BG);
Listview.setselector (drawable);

Note: The list is sometimes black, and the following code needs to be added to make it transparent:

Copy Code code as follows:
Android:cachecolorhint= "@android: Color/transparent"

I hope this article will help you with the Android program.

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.