2013-02-27 33 views
91

我在我的活動中有6個ImageButton,我通過其中的代碼(不使用xml)設置圖像。Android中的ImageButton中的適合圖像

我希望它們覆蓋按鈕區域的75%。但是,由於某些圖像覆蓋的面積較小,有些圖像太大而不適合imageButton。如何以編程方式調整大小並顯示它們? 下面是屏幕截圖

enter image description here以下 是XML文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_marginBottom="5sp" 
     android:layout_marginLeft="2sp" 
     android:layout_marginRight="5sp" 
     android:layout_marginTop="0sp"  > 
    <LinearLayout 
     android:layout_height="0dp" 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 
     <ImageButton   

      android:layout_height="match_parent" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:id="@+id/button_topleft" 
     android:layout_marginBottom="5sp" 
     android:layout_marginLeft="2sp" 
     android:layout_marginRight="5sp" 
     android:layout_marginTop="0sp" 
      /> 
     <ImageButton 
      android:layout_height="match_parent" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:id="@+id/button_topright" 
    android:layout_marginBottom="5sp" 
     android:layout_marginLeft="2sp" 
     android:layout_marginRight="5sp" 
     android:layout_marginTop="0sp" 
      /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_height="0dp" 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 

     <ImageButton 
      android:layout_height="match_parent" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:id="@+id/button_repeat" 
    android:layout_marginBottom="5sp" 
     android:layout_marginLeft="2sp" 
     android:layout_marginRight="5sp" 
     android:layout_marginTop="0sp"  
      /> 

       <ImageButton 
      android:layout_height="match_parent" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:id="@+id/button_next" 
    android:layout_marginBottom="5sp" 
     android:layout_marginLeft="2sp" 
     android:layout_marginRight="5sp" 
     android:layout_marginTop="0sp"  
      /> 

    </LinearLayout>  
    <LinearLayout 
     android:layout_height="0dp" 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 

     <ImageButton 
      android:layout_height="match_parent" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:id="@+id/button_bottomleft" 
    android:layout_marginBottom="5sp" 
     android:layout_marginLeft="2sp" 
     android:layout_marginRight="5sp" 
     android:layout_marginTop="0sp"         
      /> 
     <ImageButton 
      android:layout_height="match_parent" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:id="@+id/button_bottomright" 
    android:layout_marginBottom="5sp" 
     android:layout_marginLeft="2sp" 
     android:layout_marginRight="5sp" 
     android:layout_marginTop="0sp"     
      />   
    </LinearLayout>   

</LinearLayout> 

和 myClass.java的一個片段:

public void addImageButtons() 
    { 
     iB_topleft = (ImageButton) findViewById(R.id.button_topleft); 
     iB_topright = (ImageButton) findViewById(R.id.button_topright); 
     iB_bottomleft = (ImageButton) findViewById(R.id.button_bottomleft); 
     iB_bottomright = (ImageButton) findViewById(R.id.button_bottomright); 
     iB_next = (ImageButton) findViewById(R.id.button_next); 
     iB_repeat = (ImageButton) findViewById(R.id.button_repeat); 
    } 

    public void setImageNextAndRepeat() 
    { 

    iB_topleft .setImageResource(R.drawable.aa); 
     iB_topright.setImageResource(R.drawable.bb); 

    iB_bottomleft.setImageResource(R.drawable.cc); 
     iB_bottomright.setImageResource(R.drawable.dd); 

     iB_next.setImageResource(R.drawable.next); 
     iB_repeat.setImageResource(R.drawable.repeat);  
    } 
+1

你檢查縮放的方法Android提供? http://developer.android.com/reference/android/widget/ImageView.ScaleType.html – bofredo 2013-02-27 15:57:22

回答

268

我希望他們能夠覆蓋75%的按鈕區域。

使用android:padding="20dp"(根據需要調整填充)來控制圖像在按鈕上的佔用量。

但由於某些圖像覆蓋的面積較小,有些圖像太大而不適合imageButton。如何以編程方式調整大小並顯示它們?

使用android:scaleType="fitCenter"讓Android縮放圖像,並使用android:adjustViewBounds="true"讓它們根據縮放來調整邊界。

所有這些屬性都可以在運行時在每個ImageView的代碼中設置。然而,在我看來,在xml中設置和預覽要容易得多。

此外,不要使用sp比文字大小等什麼,這取決於文本大小偏好的用戶設定縮放,讓您的sp尺寸將大於你的意圖,如果用戶有一個「大」文本設置。改用dp,因爲它不會被用戶的文字大小首選項縮放。

這裏的每個按鈕應該是什麼樣子的一個片段:

<ImageButton 
     android:id="@+id/button_topleft" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_marginBottom="5dp" 
     android:layout_marginLeft="2dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="0dp" 
     android:layout_weight="1" 
     android:adjustViewBounds="true" 
     android:padding="20dp" 
     android:scaleType="fitCenter" /> 

Sample Button

+1

感謝它的工作。 – RDX 2013-02-28 05:27:51

+0

@Steven Byle對不起,遲到的迴應,但我怎麼能應用相同的,但相對佈局? – 2014-02-02 17:34:52

+1

它工作!保存了我的小時搜索!愛你 – 2014-08-08 05:27:15

5

嘗試在I-ImageButton的XML使用android:scaleType="fitXY"

+0

雖然這工作和縮放,但我希望它應該覆蓋75%的區域,以提高可見性。 – RDX 2013-02-27 16:57:37

+0

@PowerPC嘗試使用framelayout使高度和寬度達到75%,並進入該框架佈局使用您的imagebutton並設置適合xy – 2013-02-27 17:11:03

+0

framelayout爲每個imagebutton內linearlayout?可以üPLZ澄清 – RDX 2013-02-27 17:13:35

4

我使用android:scaleType="fitCenter"滿意。

16

我用下面的代碼在xml

android:adjustViewBounds="true" 
android:scaleType="centerInside" 
+0

優秀的答案。 – 2016-01-12 09:41:20

+0

我有一個相當「平坦」的矩形,這對我來說效果很好......雖然有一個問題:確保使用「android:src」而不是「android:background」。 +1。 – 2016-02-07 15:58:47

0

最近我發現偶然的,因爲你有一個ImageView的更多的控制,你可以設置一個onclicklistener的圖像 這裏是一個樣本動態創建的圖像按鈕

private int id; 
private bitmap bmp; 
    LinearLayout.LayoutParams familyimagelayout = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT); 

    final ImageView familyimage = new ImageView(this); 
      familyimage.setBackground(null); 
      familyimage.setImageBitmap(bmp); 
      familyimage.setScaleType(ImageView.ScaleType.FIT_START); 
      familyimage.setAdjustViewBounds(true); 
      familyimage.setId(id); 
      familyimage.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //what you want to do put here 
       } 
      }); 
0

它在我的情況下效果很好。首先,您下載一個圖像並將其重命名爲iconimage,將其定位在可繪製文件夾中。您可以通過設置android:layout_widthandroid:layout_height來更改尺寸。最後,我們有

<ImageButton 
     android:id="@+id/answercall" 
     android:layout_width="120dp" 
     android:layout_height="80dp" 
     android:src="@drawable/iconimage" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:scaleType="fitCenter" /> 
0

請參考以下鏈接,並設法找到你真正想要的東西:

ImageView.ScaleType CENTER中心圖像中的觀點,但執行 不結垢。

ImageView.ScaleType CENTER_CROP縮放圖像均勻(保持 圖像的縱橫比),使得兩個尺寸(寬度和高度)的圖像的 將等於或大於所述視圖的對應 尺寸大(負填充)。

ImageView.ScaleType CENTER_INSIDE縮放圖像均勻(保持 圖像的縱橫比),使得兩個尺寸(寬度和高度)的圖像的 會比視圖的相應尺寸 等於或小於(減去填充)。

ImageView.ScaleType FIT_CENTER使用CENTER縮放圖像。

ImageView.ScaleType FIT_END使用END縮放圖像。

ImageView.ScaleType FIT_START使用START縮放圖像。

ImageView.ScaleType FIT_XY使用FILL縮放圖像。

ImageView.ScaleType MATRIX在繪製時使用圖像矩陣進行縮放。

https://developer.android.com/reference/android/widget/ImageView.ScaleType.html