Android:如何防止图像在ImageView或ImageButton中缩放?

时间:2010-03-09 02:00:51

标签: java android imagebutton imageview

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

例如,在屏幕顶部创建一个按钮间距相等的4按钮工具栏会很有用,但即使我使用scaleType =“center”,按钮内的图像也会保持拉伸状态,这应该根据文档阻止缩放,但事实并非如此。

任何见解都表示赞赏!

谢谢,

3 个答案:

答案 0 :(得分:10)

我发现在使用android时:background会自动将你在那里使用的图像缩放到View的大小。

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

所以我尝试将整个Button设置为自己的相对布局,这就是我使用的:

<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本身可以增长和移动,按钮顶部的图像将始终保持相同的大小,但按钮将增长。但是,如果布局比图像本身小,图像将会缩小。

我认为这就是你要找的东西。可能有更好的方法来做到这一点,但这就是我现在能想到的全部内容。

答案 1 :(得分:3)

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

然后,如果您应用非透明 android:background ,则可以检查更大的按钮区域:

<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>

将上一个和新的drawable应用于 android:src ,你应该清楚地看到ImageButton区域。

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

答案 2 :(得分:2)

如果图像已缩放,请确保您没有在兼容模式下运行应用程序(例如,如果您在不支持多个密度的情况下定位Android 1.5 / 1.6并且在Android 2.0上运行该应用程序。)

相关问题