如何让Android中的ImageButton根据屏幕密度自动缩放?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2047554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:21:15  来源:igfitidea点击:

How to make ImageButton in Android automatically scale based on screen density?

androidimagebuttondpi

提问by Randy Sugianto 'Yuku'

I have an image put on an ImageButton, which looks good on a mdpi (medium-dpi) screen. However, when the device has a hdpi (high-dpi) screen, the image is still pixel-by-pixel accurate, which look small on the device screen.

我有一个图像放在ImageButton,它在 mdpi(中等 dpi)屏幕上看起来不错。但是,当设备具有 hdpi(高 dpi)屏幕时,图像仍然是逐像素精确的,在设备屏幕上看起来很小。

How do I make such that the image (and therefore the ImageButton) scaled based on the density-independent pixels (dpor dip) instead of pixels?

如何使图像(以及ImageButton)基于与密度无关的像素(dpdip)而不是像素进行缩放?

回答by Michael

android:scaleType="fitXY"

Other choices include :

其他选择包括:

fitStart
fitCenter
fitEnd
center
cenerCrop
centerInside
matrix

Mind you i'm lazy and use really large PNG images in a raw folder.

请注意,我很懒惰,并且在原始文件夹中使用了非常大的 PNG 图像。

回答by ponkin

IMHO you should look pretty close to NinePatchDrawable. There is, actually, a toolthat will help you to make your icon(pictures/icons/whatever...) resizable depending on size of container. It`s really simple and works just fine for my needs. Also look at this article(part "NinePatchDrawable"), it explains the NinePatchDrawable mechanism.

恕我直言,你应该看起来非常接近NinePatchDrawable。实际上,有一种工具可以帮助您根据容器的大小调整图标(图片/图标/任何...)的大小。它真的很简单,并且可以很好地满足我的需求。另请看这篇文章(“NinePatchDrawable”部分),它解释了 NinePatchDrawable 机制。

A NinePatchDrawable graphic is a stretchable bitmap image, which Android will automatically resize to accomodate the contents of the View in which you have placed it as the background. An example use of a NinePatch is the backgrounds used by standard Android buttons — buttons must stretch to accommodate strings of various lengths. A NinePatch drawable is a standard PNG image that includes an extra 1-pixel-wide border. It must be saved with the extension .9.png, and saved into the res/drawable/ directory of your project.

NinePatchDrawable 图形是可拉伸的位图图像,Android 会自动调整其大小以适应您将其作为背景放置的视图的内容。一个使用 NinePatch 的示例是标准 Android 按钮使用的背景——按钮必须拉伸以适应不同长度的字符串。NinePatch drawable 是标准的 PNG 图像,包含额外的 1 像素宽边框。它必须以扩展名 .9.png 保存,并保存到项目的 res/drawable/ 目录中。

回答by Roman Nurik

As ponkin mentioned, 9-patches are your best bet. Combined with resource directory qualifiers (drawable-hdpi, drawable-mdpi, etc.) you can make assets that look good at any density and size.

正如 ponkin 所提到的,9-patches 是你最好的选择。结合资源目录限定符(drawable-hdpidrawable-mdpi等),您可以制作在任何密度和大小下看起来都不错的资产。

An example of 9-patches and resource directory qualifiers can be found in the Multiple ResolutionsAndroid SDK sample. The semi-transparent, black, rounded-corner background is implemented as a 9-patch with different PNGs for ldpi, mdpi, and hdpi.

可以在多分辨率Android SDK 示例中找到 9 补丁和资源目录限定符的示例。半透明、黑色、圆角背景作为 9-patch 实现,具有用于 ldpi、mdpi 和 hdpi 的不同 PNG。

回答by Fedor

Maybe you should just specify button size in device-independent-pixels?

也许您应该只在与设备无关的像素中指定按钮大小?

android:layout_width="300dip" 
android:layout_height="50dip"