如何让Android中的ImageButton根据屏幕密度自动缩放?
我将图像放在 ImageButton
上,该图像在 mdpi (medium-dpi) 屏幕上看起来不错。但是,当设备具有 hdpi(高 dpi)屏幕时,图像仍然是逐像素精确的,这在设备屏幕上看起来很小。
如何使图像(以及 ImageButton
)基于与密度无关的像素(dp 或 dip)而不是像素?
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.
How do I make such that the image (and therefore the ImageButton
) scaled based on the density-independent pixels (dp or dip) instead of pixels?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
其他选择包括:
请注意,我很懒,在原始文件夹中使用非常大的 PNG 图像。
Other choices include :
Mind you i'm lazy and use really large PNG images in a raw folder.
正如 ponkin 提到的,9 块是你最好的选择。与资源目录限定符(
drawable-hdpi
、drawable-mdpi
等)相结合,您可以制作在任何密度和尺寸下都看起来不错的资源。9 补丁和资源目录限定符的示例可以在 多分辨率< /a> Android SDK 示例。半透明、黑色、圆角背景被实现为具有不同 PNG 的 9 块,适用于 ldpi、mdpi 和 hdpi。
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.An example of 9-patches and resource directory qualifiers can be found in the Multiple Resolutions Android SDK sample. The semi-transparent, black, rounded-corner background is implemented as a 9-patch with different PNGs for ldpi, mdpi, and hdpi.
恕我直言,你应该看起来非常接近 NinePatchDrawable
。实际上,有一个工具可以帮助您制作图标(图片/图标/任何...)根据容器的大小调整大小。它非常简单并且可以很好地满足我的需求。另请参阅这篇文章(“NinePatchDrawable”部分),它解释 NinePatchDrawable 机制。
IMHO you should look pretty close to NinePatchDrawable
. There is, actually, a tool that 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.
也许您应该只指定设备独立像素中的按钮大小?
Maybe you should just specify button size in device-independent-pixels?