繁体   English   中英

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

[英]Android: How to prevent image from being scaled in ImageView or ImageButton?

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

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

任何见解都表示赞赏!

谢谢,

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

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

只需修改您的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区域。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM