关于位图的显示/加载:
您需要正确加载Bitmap 并根据您的需要调整Bitmap 的大小。在大多数情况下,加载比设备屏幕支持的分辨率更高的位图是没有意义的。
此外,这种做法非常重要,以避免 OutOfMemoryErrors 在使用这么大的 Bitmaps 时。
例如,大小为 8000 x 4000 的位图使用超过 100 兆字节 的 RAM(32 位颜色),这对于移动设备来说是一个巨大的数量,远远超过即使是高端设备也能应付。
这是正确加载位图的方法:
public abstract class BitmapResLoader {
public static Bitmap decodeBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
private static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
}
代码中的示例用法:
Bitmap b = BitmapResLoader.decodeBitmapFromResource(getResources(),
R.drawable.mybitmap, 500, 500);
摘自此处的 Google Android 开发者指南:
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
关于最大位图尺寸:
最大位图大小限制取决于底层的 OpenGL 实现。使用 OpenGL 时,可以通过(来源:Android : Maximum allowed width & height of bitmap)进行测试:
int[] maxSize = new int[1];
gl.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxSize, 0);
例如对于 Galaxy S2,它是 2048x2048。