位图的getWidth返回错误值 [英] Bitmap getWidth returns wrong value

查看:111
本文介绍了位图的getWidth返回错误值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其中分辨率为1000x600我的Andr​​oid应用程序绘制文件夹中的JPG图片。 我加载图像转换为位图这样的

 点阵位图= BitmapFactory.de codeResource(getResources(),R.drawable.image);
 

这我电话后位图.getWidth()返回1​​500它是怎样呢?而如何获取图像的正确宽度和高度?

解决方案

这可能是因为不同的密度。你的资源是可能存储在一个中等密度的文件夹,但你的设备是华电国际。中密度为160dpi,高密度240dpi。因此,您的位图缩小到1.5倍它原来的尺寸。请参阅<一href="http://developer.android.com/guide/practices/screens_support.html#DensityConsiderations">document在多个屏幕上,获取更多信息。

  

如果资源不是在正确的密度可用,系统加载默认的资源和扩展它们或根据需要来匹配当前屏幕的密度下降。

如果您打算这是为高密度把绘制或绘制,MDPI它的绘制,华电国际,而不是。

更新:

如果你想让它忽略密度,把它放在一个绘制-nodpi文件夹。还从同一文档

  

要避免pre-缩放最简单的方法是将资源的资源目录与nodpi配置预选赛。例如:

  RES /绘制-nodpi / icon.png在
 

  

在本系统采用的icon.png位图从该文件夹根据当前设备的密度,它不能扩展它。

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?

解决方案

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.

这篇关于位图的getWidth返回错误值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆