位图 getWidth 返回错误值
我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是由于密度不同造成的。您的资源可能存储在中等密度文件夹中,但您的设备是 hdpi。中密度为160dpi,高密度为240dpi。因此,您的位图将缩放至原始大小的 1.5 倍。有关详细信息,请参阅多个屏幕上的文档。
如果您打算将其用于高密度,请将其放入drawable-hdpi而不是drawable或drawable-mdpi中。
更新:
如果您希望它忽略密度,请将其放入drawable-nodpi文件夹中。也来自同一个文档:
您还可以在此处查看有关提供和分组的更多信息资源。
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 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:
You can also see here for more information on providing and grouping resources.