文件中的位图图像在高密度设备上无法缩放
我在高密度屏幕 (480x800) 上的 imageview 上显示位图图像时遇到问题。从 sdcard 上的文件加载位图图像时,图像不会缩放以适合 hdpi 屏幕。在中等密度屏幕上它可以正常工作(320x480)。
public static Bitmap getBitmapFromFile(String src) {
File file = new File(src);
if (file.exists()) {
return BitmapFactory.decodeFile(src);
}
return null;
}
mImage.setImageBitmap(Util.getBitmapFromFile(filePath));
hdpi 和 屏幕截图平均密度指数 http://202.148.2.34/~lorenz/iview.jpg
I have problem with displaying bitmap image on imageview on high density screen (480x800). When the bitmap image loaded from file on sdcard, the image does not scale to fit hdpi screen. On medium density screen it works normal (320x480).
public static Bitmap getBitmapFromFile(String src) {
File file = new File(src);
if (file.exists()) {
return BitmapFactory.decodeFile(src);
}
return null;
}
mImage.setImageBitmap(Util.getBitmapFromFile(filePath));
Screenshoot on hdpi & mdpi
http://202.148.2.34/~lorenz/iview.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试向其中添加缩放类型
有很多可用的缩放类型。检查哪一个适合您。
Try adding a scale type to it
There are lot of scaling type available. Check which one suits you.
这是一个老问题,但我认为有一种更简单的方法可以做到这一点。看起来,当您从外部源(不在您的
/res
文件夹中)加载位图时,Android 框架会将其视为适用于基线设备(即 mdpi)。因此,它在 320x480 mdpi 屏幕上看起来很棒,但在 hdpi 屏幕上则不然。现在系统通常应该在 hdpi 屏幕上缩放 1.5 倍(除非您在位图对象中设置了密度,但我真的不知道它是如何工作的),但因为您使用了方法
setImageBitmap() 位图未缩放,导致您的问题。
假设如果您使用方法
setImageDrawable()
并将位图包装在Drawable
中,则位图将按照您想要的方式自动缩放。我还没有尝试过这个,但显然 这个人< /a> 有与你相反的问题。
This is an old question but I think there is an easier way to do this. It seems that when you load a bitmap from an external source (one not in your
/res
folder) the Android framework will treat it as if it was meant for the baseline device (ie mdpi). Therefore it looks great on a 320x480 mdpi screen, but not as much on an hdpi screen.Now the system normally should scale for this on an hdpi screen by a factor of 1.5 (unless you supposedly you set a density in the bitmap object, but I don't really know how that works) but since you used the method
setImageBitmap()
the bitmap is not scaled, leading to your problem.Supposedly if you use the method
setImageDrawable()
and wrap the bitmap in aDrawable
the bitmap will be autoscaled just like you wanted.I haven't tried this, but apparently this guy had the opposite problem of you.