2014-09-10 71 views
0

我通過「資源選擇器」創建一個新的ImageButton和我結束了這段代碼:如何在圖像變得模糊的情況下拉伸ImageButton?

<ImageButton 
    android:id="@+id/imageButton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:layout_marginLeft="14dp" 
    android:layout_marginTop="94dp" 
    android:src="@drawable/mybutton" /> 

現在,我想打那個按鈕的方式做大,所以我做了以下修改:

<ImageButton 
    android:id="@+id/imageButton1" 
    android:layout_width="120dp" 
    android:layout_height="120dp" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:layout_marginLeft="14dp" 
    android:layout_marginTop="94dp" 
    android:src="@drawable/aries" /> 

我也將android:src="@drawable/mybutton"更改爲android:background="@drawable/mybutton",以擺脫我的圖像周圍的灰色按鈕,而不是拉伸圖像。

問題:

我的問題是,伸展出的圖像時,它仍然使用從drawable-mdpi文件(因爲它應該),所以我結束了一個拉伸低分辨率圖像。如果我從drawable-mdpi文件夾更改高分辨率文件夾的圖像,它看起來不錯,但我確信這不是最好的解決方案,因爲我必須在每個文件夾中放置分辨率更高的圖像。

回答

1

聽起來像你需要的是一個Nine-Patch Drawable

NinePatch是一個PNG圖像,您可以在該圖像中定義Android在視圖內的內容超出正常圖像邊界時可伸縮的區域。 [...]當視圖增長以適應內容時,Nine-Patch圖像也會縮放以匹配View的大小。

+0

我只是測試它。它很棒!謝謝! – 2014-09-10 14:54:27

0

嘗試的背景設置爲null

<ImageButton 
    android:id="@+id/imageButton1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@null" 
    android:src="@drawable/ic_launcher" /> 
+0

這使得灰色背景透明。我需要在它上面展開圖片。 – 2014-09-10 14:34:32

+1

然後@Nicklas是對的,你必須使用[Nine Patch](http://romannurik.github.io/AndroidAssetStudio/nine-patches.html) – 2014-09-10 14:52:31