目前我正在開發一款應用程序,該應用程序應該可以在平板電腦和手機上運行。 我的主菜單背景圖像的分辨率是2000x1250,根據畫布寬度和高度調整大小。圖像的大小是320KB(PNG)。但是,如果分辨率爲2000x1250,dalvikm分配器將爲此單個背景圖像分配高達30MB的內存。我已閱讀並應用文章http://developer.android.com/training/displaying-bitmaps/load-bitmap.html,但分辨率爲2000x1250時(我在S3上以1280x720的分辨率測試此內存)時,內存容量保持不變。這是我應該擔心的事嗎?平板電腦有更多空間用於更大的位圖嗎?320kb的背景圖片佔用了大約30MB的內存空間
由於提前, -Z
EDIT1:
logcat的
應用開始
02-08 20:35:12.105:d/dalvikvm(27962):GC_FOR_ALLOC已釋放8804K,18%自由53293K/64775K,暫停31毫秒,31毫秒總
02-08 20:35:12.110:I/dalvikvm堆(27962):生長堆(FRAG情況下),以61.693MB爲9000016字節分配
02-08 20:35:12.145:D/dalvikvm(27962):GC_CONCURRENT已釋放35206K,59%空閒26875K/64775K,暫停11ms + 3ms,總計32ms
02-08 20:35:12.315:D/dalvikvm (27962):GC_FOR_ALLOC釋放0K,免費26875K/64775K 59%,暫停時間10ms,10ms的總
這就是主菜單雙頭呆
02-08 20:35:12.330:I/dalvikvm堆(27962):生長堆(FRAG情況下),以61.643MB爲36000016字節分配
02- 08 20:35:12.350:d/dalvikvm(27962):GC_CONCURRENT釋放0K,5%的遊離62031K/64775K,暫停11毫秒+ 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(做不介意棄用,這不是問題):在XML
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)));
,代碼:
<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"
>
任何想法?
內存中的位圖是未壓縮的,這就是爲什麼它非常大。您是否嘗試了鏈接到的文章中描述的技術,加載了位圖的縮小版本?如果您的設備是1280x720,則不需要2000x1250位圖。 – Clyde 2013-02-08 19:28:25
那麼如果我問詹姆斯,那麼你如何建議在高分辨率背景下加載?爲平板電腦和手機制作兩種不同的版本? – Zimon 2013-02-08 19:46:33
我看不出有這麼高的內存使用率的原因。 – rekire 2013-02-08 20:01:19