gpt4 book ai didi

java - 位图 getWidth 返回错误值

转载 作者:IT老高 更新时间:2023-10-28 20:51:06 24 4
gpt4 key购买 nike

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

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

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

最佳答案

这可能是因为密度不同。您的资源可能存储在中等密度的文件夹中,但您的设备是 hdpi。中密度为160dpi,高密度为240dpi。因此,您的位图被缩放到原来大小的 1.5 倍。见 document on multiple screens了解更多信息。

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.

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

更新:

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

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.

关于java - 位图 getWidth 返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8198291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com