BitmapFactory returns bigger image than source

老子叫甜甜 提交于 2019-11-29 03:05:45

问题


Hi i am creating a Bitmap from an png image named image.png. The image has the dimension 75 (width) x 92 (height). When I run this code:

Bitmap bitmap = BitmapFactory.decodeResource(this.context.getResources(),  R.drawable.image
Log.d("image", "height: " + bitmap.getHeight() + " width: " + bitmap.getWidth());

the logger logs:

DEBUG/image(3550): height: 138 width: 113

and the image on the screen is bigger than other images which have the dimension 75 x 92. What can I do to make android load the image with the right dimension?


回答1:


My solution:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;               
Bitmap bitmap = BitmapFactory.decodeResource(this.context.getResources(),  R.drawable.image, options );



回答2:


It sounds like your screen density on your device is different than the density where image.png was created.

If you really want to prevent the scaling, you could try one of the following:

  1. Put the image in res/drawable-nodpi (http://developer.android.com/guide/practices/screens_support.html#qualifiers)

  2. Use ImageView.ScaleType.CENTER (http://developer.android.com/reference/android/widget/ImageView.ScaleType.html)

  3. Just found this related question on SO: Android: How to stop Android 1.6+ from scaling images




回答3:


Beause loader in BitmapFactory applies screen density scaling during loading. To override this, provide own desired inTargetDensity in BitmapFactory.Options in call to decodeResource.




回答4:


Bitmap thumbImage = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.image), 640, 640, false);



来源:https://stackoverflow.com/questions/7363643/bitmapfactory-returns-bigger-image-than-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!