程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> imageview-android imagebutton背景圖片設置

imageview-android imagebutton背景圖片設置

編輯:編程綜合問答
android imagebutton背景圖片設置

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>
danielinbiti
danielinbiti
llx2007
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved