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>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。