要在Android ImageView添加点击效果,可以通过以下几种方法:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/image_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/image_focused" android:state_focused="true"/>
<item android:drawable="@drawable/image_normal"/>
</selector>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@android:color/white"/>
</shape>
</item>
</ripple>
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.9f, 1.0f, 0.9f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(100);
imageView.startAnimation(scaleAnimation);
}
});
通过上述方法,可以为Android ImageView添加不同的点击效果,让用户在点击时有视觉上的反馈。