ImageView切换两种状态下的模式

1、xml对ImageView的src要设置成selector

 <ImageView
                android:id="@+id/like_icon"
                android:layout_width="@dimen/will_like_icon_width"
                android:layout_height="@dimen/will_like_icon_height"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:src="@drawable/selector" />

drawable/selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/clickgreat" android:state_pressed="true"/>
    <item android:drawable="@drawable/clickgreat" android:state_selected="true"/>
    <item android:drawable="@drawable/great"/>
</selector>

 切换图片

1 //深色
2 likeIcon.setSelected(true);
3 //浅色
4 likeIcon.setSelected(false);

Done

posted @ 2016-02-25 21:45  行云有影  阅读(2158)  评论(0编辑  收藏  举报