Android programming simple implementation ImageView click when the background image modification method

  • 2020-11-18 06:27:00
  • OfStack

An example of Android programming is presented in this paper, which simply realizes the background image modification when ImageView clicks. To share for your reference, the details are as follows:

When using ImageView, you want the background image to change 1 when it is clicked, so that it will be more obvious when it is clicked. Here is a very simple way, or at least a very clear way. Create 1 xml file under the res/drawable folder. my. xml, for example, reads 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>

One thing to note here is that drawable above refers to the system's image resource. If you are using your own image resource, use the following format

android:drawable="@drawable/ic_desk_point_normal"

Then, in the xml file that defines imageView, set:


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

The above will complete the effect.

Remember to add clickable="true" or selector won't work.

Save the XML below as a.xml file (such as list_item_bg.xml) and the runtime system will use the background image based on the state of the list item in ListView.

drawable/list_item_bg.xml


<?xml version="1.0" encoding="utf-8" ?>   
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 <!--  The default background image  -->  
 <item android:drawable="@drawable/pic1" /> 
 <!--  Background image with no focus  -->  
 <item android:state_window_focused="false"  
    android:drawable="@drawable/pic1" />   
 <!--  Background image when you get focus and click in non-touch mode  -->  
 <item android:state_focused="true" android:state_pressed="true"   
    android:drawable= "@drawable/pic2" />  
 <!--  Click on the background image in touch mode  -->  
 <item android:state_focused="false" android:state_pressed="true"   
    android:drawable="@drawable/pic3" />   
 <!-- The background of the image when selected  -->  
 <item android:state_selected="true"   
    android:drawable="@drawable/pic4" />   
 <!-- The background of the image when it is in focus  -->  
 <item android:state_focused="true"   
    android:drawable="@drawable/pic5" />   
</selector>

Usage:

The first is configured in listview

android:listSelector="@drawable/list_item_bg"

The second is to add attributes to item of listview
android:background="@drawable/list_item_bg"

The third is used in the java code:


Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg);
listview.setSelector(drawable);

Note: Lists are sometimes black, and you need to add the following code to make them transparent:

android:cacheColorHint="@android:color/transparent"

I hope this article has been helpful in Android programming.


Related articles: