gridview的布局xml有3個imagebutton,屬性android:src="@drawable/on",(用background背景圖片變形了,只能用src),點擊後圖像重疊並且變形了,怎麼辦?
voice.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="@drawable/voice_on_unpress" /> <!-- pressed -->
<item android:state_pressed="true"
android:drawable="@drawable/voice_on_do" /> <!-- default -->
<item android:state_pressed="true"
android:drawable="@drawable/voice_on_do" /> <!-- default -->
<item
android:drawable="@drawable/voice_on_unpress" />
</selector>
voice_press.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="@drawable/voice_on_press" /> <!-- pressed -->
<item android:state_pressed="true"
android:drawable="@drawable/voice_on_do" /> <!-- default -->
<item android:state_pressed="true"
android:drawable="@drawable/voice_on_do" /> <!-- default -->
<item
android:drawable="@drawable/voice_on_press" />
</selector>
activity.java中
search_voice.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if(flag){
Toast.makeText(getApplicationContext(), "語音開啟",Toast.LENGTH_SHORT).show();
search_voice.setBackgroundResource(R.drawable.vocie_press);
flag = false;
}
else{
Toast.makeText(getApplicationContext(), "語音屏蔽",Toast.LENGTH_SHORT).show();
search_voice.setBackgroundResource(R.drawable.vocie);
flag = true;
}
}
});
圖形變形是你image設置的scaletype引起,這涉及到圖片剪裁的問題。如果圖片四周是對稱可伸縮,用9patch處理一下圖片。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="@drawable/voice_on_unpress" /> <!-- pressed -->
<item android:state_pressed="true"
android:drawable="@drawable/voice_on_do" /> <!-- default -->
</selector>