【发布时间】:2014-07-10 10:11:57
【问题描述】:
我有一张高分辨率 (1188 * 17617 2.35MB) 图像,我想将它加载到我的 Android 应用程序中。 如果我使用 WebView,图像可以显示。但它是模糊的。我用安卓操作系统的浏览器打开它,它也模糊不清。 如果我使用 ImageView,它什么也不显示。
【问题讨论】:
-
缩小图像是唯一的解决方案......
标签: android resolution
我有一张高分辨率 (1188 * 17617 2.35MB) 图像,我想将它加载到我的 Android 应用程序中。 如果我使用 WebView,图像可以显示。但它是模糊的。我用安卓操作系统的浏览器打开它,它也模糊不清。 如果我使用 ImageView,它什么也不显示。
【问题讨论】:
标签: android resolution
如果位图太大,您可以先按比例缩小位图,然后再将它们加载到内存中。您可以通过使用 BitmapFactory.Options 来实现这一点,并根据您的要求进行缩减。
public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeFile(path, options);
return bmp;
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
【讨论】:
使用毕加索图书馆。 http://square.github.io/picasso/.
它为您处理所有事情,包括下载、缓存,最重要的是,您可以通过调整大小功能进行下采样。它还可以与标准的 ImageView 一起使用,只需很少的代码:
毕加索.with(上下文) .load(网址) .resize(50, 50) .centerCrop() .into(imageView)
【讨论】: