位图 getWidth 返回错误值

发布于 2024-12-17 04:39:38 字数 256 浏览 3 评论 0原文

我的 android 应用程序可绘制文件夹中有一张 jpg 图像,分辨率为 1000x600。 我像这样将该图像加载到位图

 Bitmap bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.image);

之后,我调用 bitMap .getWidth() 它返回 1500。这是怎么回事?以及如何获得正确的图像宽度和高度?

I have a jpg image in my android application drawable folder which resolution is 1000x600.
I load that image to bitmap like this

 Bitmap bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.image);

After this I call bitMap .getWidth() which returns 1500. How it can be? And how to get the right width and height of image?

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

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

发布评论

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

评论(1

温馨耳语 2024-12-24 04:39:38

这可能是由于密度不同造成的。您的资源可能存储在中等密度文件夹中,但您的设备是 hdpi。中密度为160dpi,高密度为240dpi。因此,您的位图将缩放至原始大小的 1.5 倍。有关详细信息,请参阅多个屏幕上的文档

如果资源无法以正确的密度提供,系统会加载默认资源并根据需要放大或缩小资源以匹配当前屏幕的密度。

如果您打算将其用于高密度,请将其放入drawable-hdpi而不是drawable或drawable-mdpi中。

更新:

如果您希望它忽略密度,请将其放入drawable-nodpi文件夹中。也来自同一个文档:

避免预缩放的最简单方法是将资源放入带有 nodpi 配置限定符的资源目录中。例如:

res/drawable-nodpi/icon.png

当系统使用此文件夹中的 icon.png 位图时,它不会根据当前设备密度对其进行缩放。

您还可以在此处查看有关提供和分组的更多信息资源。

This is probably because of differing densities. Your resource is probably stored in a medium density folder, but your device is hdpi. Medium density is 160dpi, high density is 240dpi. Thus your bitmap is scaled to 1.5x the size it was originally. See the document on multiple screens for more info.

If resources are not available in the correct density, the system loads the default resources and scales them up or down as needed to match the current screen's density.

If you intended this to be for high density put it in drawable-hdpi instead of drawable or drawable-mdpi.

Update:

If you want it to ignore density, put it in a drawable-nodpi folder. Also from the same doc:

The easiest way to avoid pre-scaling is to put the resource in a resource directory with the nodpi configuration qualifier. For example:

res/drawable-nodpi/icon.png

When the system uses the icon.png bitmap from this folder, it does not scale it based on the current device density.

You can also see here for more information on providing and grouping resources.

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