要在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添加不同的點擊效果,讓用戶在點擊時有視覺上的反饋。