很简单的代码测试一个图片的尺寸:
Bitmap img1 = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
int w = img1.getWidth();
int h = img1.getHeight();
System.out.printf("++++++++++++++++++ w=" + w + " h=" + h);
但有时会发现获取的尺寸比其实际尺寸要大1/3(如原图是300x300,则上面代码返回的是400x400)
要是突然遇到这种问题有时是会让人有些抓狂的。。。
具体原因可能是选择的虚拟机类型变了导致的(还不太确定),解决方法如下:
You have two options, use the BitmapFactory + BitmapFactory.Options like this:
BitmapFactory.Options bfoOptions = new BitmapFactory.Options();
bfoOptions.inScaled = false;
BitmapFactory.decodeResource( rMgr, ResID, bfoOptions );
Or you can put all the resources you don't want scaled in the drawable-nodpi
directory here:
Project/res/drawable-nodpi/resource.png
I've used them both and they seem to both work equally well. If you want the
option of deciding to scale or not via a preference or similar, the code
edition would probably be the better choice.
BitmapFactory.Options bfoOptions = new BitmapFactory.Options();
bfoOptions.inScaled = false;
BitmapFactory.decodeResource( rMgr, ResID, bfoOptions );
Or you can put all the resources you don't want scaled in the drawable-nodpi
directory here:
Project/res/drawable-nodpi/resource.png
I've used them both and they seem to both work equally well. If you want the
option of deciding to scale or not via a preference or similar, the code
edition would probably be the better choice.
示例代码:
BitmapFactory.Options bfoOptions = new BitmapFactory.Options();
bfoOptions.inScaled = false;
Bitmap img1 = BitmapFactory.decodeResource(getResources(), R.drawable.sb, bfoOptions);
w = img1.getWidth();
h = img1.getHeight();
System.out.printf("++++++++++++++++++ w=" + w + " h=" + h);
这次得到绝对是正确的!
当然了,根据上面提示也可以自建一个drawable-nodpi目录存放文件(测试是可以的,貌似更简单!)
还有一点:直接从文件系统中解析图片文件(如/sdcard/test.jpg)好像不受影响,获得width/height都是正常的。
更多描述:groups.google.com/group/android-develope...f4?#9be5b95022d17df4