在Android开发中使用ImageView时,希望当图片被点击时有明显的效果,一个简单的方法就是在点击时更换图片。下面以注销按钮为例,效果和实现方法如下:
首先,再res/drawable文件夹下创建一个xml文件,命名为logoutimage.xml,在该文件中实该图片在点击前和点击时的图片,代码如下:
1
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
4
<
item
android:state_pressed
=
"false"
5
android:drawable
=
"@drawable/logout2"
/>
7
<
item
android:state_pressed
=
"true"
8
android:drawable
=
"@drawable/logout1"
/>
然后在ImageView的src属性中引用该文件,代码如下:
2
android:src
=
"@drawable/logoutimage"
3
android:layout_width
=
"wrap_content"
4
android:layout_height
=
"wrap_content"
更多情况:
02
<
item
android:drawable
=
"@drawable/img1"
/>
05
<
item
android:state_window_focused
=
"false"
06
android:drawable
=
"@drawable/img2"
/>
09
<
item
android:state_focused
=
"true"
10
android:drawable
=
"@drawable/img3"
/>
13
<
item
android:state_selected
=
"true"
14
android:drawable
=
"@drawable/img4"
/>
17
<
item
android:state_focused
=
"false"
18
android:state_pressed
=
"true"
19
android:drawable
=
"@drawable/img5"
/>
22
<
item
android:state_focused
=
"true"
23
android:state_pressed
=
"true"
24
android:drawable
=
"@drawable/img6"
/>