目前我正在开发一个应该在平板电脑和手机上运行的应用程序。我的主菜单背景图像的分辨率是 2000x1250,它会根据画布的宽度和高度进行调整。图像大小为 320KB (PNG)。但是,如果分辨率为 2000x1250,dalvikm 分配器会为这个单一背景图像分配高达 30MB 的内存。我已经阅读并应用了文章http://developer.android.com/training/displaying-bitmaps/load-bitmap.html,但是当分辨率为 2000x1250 时,位于的内存量保持不变(我在我的S3 分辨率为 1280x720)。这是我应该担心的事情吗?平板电脑是否有更多空间容纳更大的位图?
在此先感谢,-Z
编辑1:
LOGCAT
申请开始
02-08 20:35:12.105: D/dalvikvm(27962): GC_FOR_ALLOC 释放 8804K,18% 释放 53293K/64775K,暂停 31ms,总共 31ms
02-08 20:35:12.110:I/dalvikvm-heap(27962):将堆(碎片情况)增加到 61.693MB,分配 9000016 字节
02-08 20:35:12.145: D/dalvikvm(27962): GC_CONCURRENT freed 35206K, 59% free 26875K/64775K, paused 11ms+3ms,总共32ms
02-08 20:35:12.315: D/dalvikvm(27962): GC_FOR_ALLOC 释放 0K,59% 释放 26875K/64775K,暂停 10ms,总共 10ms
这是主菜单打开的地方
02-08 20:35:12.330:I/dalvikvm-heap(27962):将堆(碎片情况)增加到 61.643MB,分配 36000016 字节
02-08 20:35:12.350: D/dalvikvm(27962): GC_CONCURRENT freed 0K, 5% free 62031K/64775K, paused 11ms+3ms,总共23ms
教程中的功能:
public 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;
}
public static Bitmap decodeSampledBitmapFromResource(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);
}
我在 OnCreate 中的代码(不要介意弃用,这不是问题):
final FrameLayout fr = (FrameLayout) findViewById(R.id.mainmenu_background);
//fr.setBackgroundResource(R.drawable.background_main_menu);
Display display = getWindowManager().getDefaultDisplay();
//Point size = new Point();
//display.getSize(size);
fr.setBackgroundDrawable(new BitmapDrawable(getResources(), decodeSampledBitmapFromResource(getResources(), R.drawable.background_main_menu, 2000, 1250)));
XML中的代码:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainmenu_background"
>
有任何想法吗?