【发布时间】:2014-08-15 05:50:00
【问题描述】:
我正在使用 Universal Image Loader 加载图像,我想对其进行缩放,以使宽度为屏幕的宽度,并且相应地缩放高度,这意味着在我加载图像之前,我知道宽度我想要,但不是高度。因此,当我加载图像时,我想获取图像的高度和宽度,并根据屏幕的宽度使用它来缩放它。我用来做这个的代码是这样的:
try {
display.getSize(size);
scaledWidth = size.x;
} catch (java.lang.NoSuchMethodError ignore) {
scaledWidth = display.getWidth();
}
String filePath = "file://" + getBaseContext().getFilesDir().getPath().toString() + "/" + imagePath + ".png";
Bitmap bitmap = imageLoader.loadImageSync(filePath);
int height = bitmap.getHeight();
int width = bitmap.getWidth();
scaledHeight = (int) (((scaledWidth * 1.0) / width) * height);
//Code to resize Bitmap using scaledWidth and scaledHeight
使用通用图像加载器调整位图大小的最佳方法是什么,或者甚至更好,有没有一种方法可以只指定宽度并根据其比例正确缩放位图?
【问题讨论】:
-
我建议您使用Picasso library。它允许非常轻松的图像加载和操作,同时仍然很灵活。
-
我的解决方案是基于@nitesh-goel 代码和@zhaoyuanjie
DisplayImageOptions但使用ImageScaleType.EXACTLY_STRETCHED。请注意,如果放大太多,图像会显得模糊。
标签: android bitmap universal-image-loader