2010-03-09 91 views
9

如果使用“fill_parent”或使用“weight”拉伸视图或按钮,如何防止我的位图在ImageView或ImageButton中自动缩放?Android:如何防止图像在ImageView或ImageButton中缩放?

例如,如果在屏幕顶部创建按钮间隔相等的4按钮工具栏,但即使使用scaleType =“center”,按钮内部的图像也会变得拉长,它应该防止根据文档进行缩放,但它不会。

任何见识都被赞赏!

感谢,

回答

10

我发现在使用android:background时,会自动将您使用的图像缩放到视图的大小。

我尝试使用android:src将图像放入视图中。图像没有缩放,但我无法将图像置于视图大小的中心位置。

所以我试图使整个按钮它自己的相对布局,这是我用什么:

<RelativeLayout android:id="@+id/RelativeLayout01" 
       android:layout_height="fill_parent" 
       android:layout_width="fill_parent"> 
       <ImageButton android:id="@+id/ImageButton01"  
          android:layout_height="fill_parent" 
          android:layout_width="fill_parent"></ImageButton> 
       <ImageView android:id="@+id/ImageView01" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:background="@drawable/cardback1" 
          android:layout_centerInParent="true"></ImageView> 
</RelativeLayout> 

的ImageButton的是在后台永远是大小的布局相同。 但ImageView将始终保持居中在RelativeLayout中,并且不会缩放。 通过这种方式,RelativeLayout本身可以增长和移动,并且按钮顶部的图像将始终保持相同的大小,但该按钮将会增长。然而,如果布局变得比图像本身小,图像将缩小。

我想这就是你要找的。可能有更好的方法来做到这一点,但这是我现在可以提出的。

+17

有没有必要这样做,只是不要使用android:background。使用android:src(ImageView)或android:drawableLeft/Right/Bottom/Top(Button或ImageView)或android:button(ImageButton)。 – 2010-03-09 08:36:49

+0

@Romain:我们不能使用9-补丁drawable作为按钮背景,我们将按钮的图标部分标记为不可伸缩,并将其周围的所有额外空间(图标周围的填充)标记为可拉伸?所以当背景重新调整大小时,只有额外部分被拉伸? – Samuh 2010-03-09 10:20:40

+1

感谢Matt和Romain,android:src正是我一直在寻找的。这就是诀窍:-) – user277827 2010-03-09 13:44:10

2

如果对图像进行缩放,请确保您不是在兼容模式下运行你的应用程序(例如,如果你的目标是Android 1.5/1.6不支持多种密度和运行在Android 2.0的应用程序。 )

3

只需修改您的drawable,应用更大的透明背景。假设我在hdpi中的drawable是41 * 48 px icon.png。我用透明背景创建了一个60 * 60像素的新图像,复制粘贴previous icon.png并保存在同一个名称下。这样你就不需要触摸你的布局。

然后,你可以检查一下按键区更大,如果你申请一个非透明安卓背景

<ImageButton android:id="@+id/widget_open" 
android:src="@drawable/icon_widget_arrow_up" 
android:background="#ff777777" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content"></ImageButton> 

应用以前和新绘制到机器人:SRC,你应该清楚地看到ImageButton区域。

顺便说一句,我在兼容模式下运行应用程序。

+0

不必要的透支,不建议使用 – Asthme 2015-04-17 20:41:49