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

发布于 2024-08-24 07:58:02 字数 219 浏览 2 评论 0原文

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

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

任何见解表示赞赏!

谢谢,

How can I prevent my bitmap from being scaled automatically in an ImageView or ImageButton if the view or button is stretched using "fill_parent" or using "weight"?

This will be useful, for example, to create a 4-button toolbar at the top of the screen where the buttons are equally spaced, but the images inside the buttons keep getting streched even if I use scaleType="center", which should prevent scaling according to the doc, but it doesn't.

Any insight is appreciated!

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水中月 2024-08-31 07:58:02

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

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

I have found that when using android:background automatically scales the image you are using there to the size of the View.

I tried using the android:src to put the image in the view. The image did not scale, but I could not get the image to center relative to the size of the View.

So I tried making the whole Button it's own relative layout and this is what I used:

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

The ImageButton is in the background and will always be the same size as the layout.
The ImageView however will always stay centered in the RelativeLayout and will not scale.
This way the RelativeLayout itself can grow and move and the Image on top of the button will always stay the same size, but the button will grow. The image will however shrink if the Layout becomes smaller than the image itself.

I think that's what you were looking for. There may be a better way to do this, but this is all I can come up with right now.

帅的被狗咬 2024-08-31 07:58:02

只需修改您的绘图,应用更大的透明背景即可。假设我的 hdpi 绘图是 41*48 px icon.png 。我创建了一个带有透明背景的 60*60 px 的新图像,复制粘贴之前的 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>

将以前的和新的可绘制对象应用到 android:src ,您应该清楚地看到 ImageButton 区域。

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

Simply modify your drawable, apply a larger transparent background. Say my drawable in hdpi is 41*48 px icon.png . I created a new image 60*60 px with a transparent background , copy-paste the previous icon.png and save under the same name. This way you don't need to touch your layouts .

Then you can check the button zone larger if you apply a non transparent 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>

Apply the previous and new drawable to android:src , you should clearly see ImageButton area.

By the way, I am running the app in compatibility mode.

夜未央樱花落 2024-08-31 07:58:02

如果图像已缩放,请确保您没有在兼容模式下运行应用程序(例如,如果您的目标平台是 Android 1.5/1.6,但不支持多种密度,而您在 Android 2.0 上运行应用程序。)

If the image is scaled, make sure you are not running your app in compatibility mode (for instance if you target Android 1.5/1.6 without supporting multiple densities and you run the app on Android 2.0.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文