BitmapFactory 返回比源更大的图像
您好,我正在从名为 image.png
的 png 图像创建位图。该图像的尺寸为 75(宽)x 92(高)。当我运行此代码时:
Bitmap bitmap = BitmapFactory.decodeResource(this.context.getResources(), R.drawable.image
Log.d("image", "height: " + bitmap.getHeight() + " width: " + bitmap.getWidth());
记录器记录:
DEBUG/image(3550): height: 138 width: 113
并且屏幕上的图像比尺寸为 75 x 92 的其他图像大。我该怎么做才能使 android 加载具有正确尺寸的图像?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的解决方案:
My solution:
听起来您设备上的屏幕密度与创建 image.png 的密度不同。
如果您确实想防止缩放,可以尝试以下操作之一:
将图像放入 res/drawable-nodpi (http://developer.android.com/guide/practices/screens_support.html#qualifiers)
使用 ImageView.ScaleType.CENTER (http://developer.android.com/reference/android/widget/ImageView.ScaleType.html)
只是发现这个相关问题:Android:如何停止Android 1.6+ 缩放图像
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:
Put the image in res/drawable-nodpi (http://developer.android.com/guide/practices/screens_support.html#qualifiers)
Use ImageView.ScaleType.CENTER (http://developer.android.com/reference/android/widget/ImageView.ScaleType.html)
Just found this related question on SO: Android: How to stop Android 1.6+ from scaling images
因为 BitmapFactory 中的加载器在加载期间应用屏幕密度缩放。
要覆盖此设置,请在调用decodeResource 时在BitmapFactory.Options 中提供自己所需的inTargetDensity。
Beause loader in BitmapFactory applies screen density scaling during loading.
To override this, provide own desired inTargetDensity in BitmapFactory.Options in call to decodeResource.