2014-09-10 70 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