這是情況:我有一個ViewAnimator有三種不同的佈局。每個佈局具有不同的背景圖像。對於xhdpi設備,圖像大小爲1280x800像素,大小約爲150KB。 現在的問題:當我開始用尺寸爲1280x800和320dp密度模擬器,我總是收到此異常:Android ViewAnimator和OutOfMemoryError
09-02 08:22:46.408: D/dalvikvm(1291): GC_EXTERNAL_ALLOC freed 428K, 51% free 2874K/5831K, external 18484K/20407K, paused 83ms
09-02 08:22:46.419: E/dalvikvm-heap(1291): 1366400-byte external allocation too large for this process.
09-02 08:22:46.498: I/dalvikvm-heap(1291): Clamp target GC heap from 25.089MB to 24.000MB
09-02 08:22:46.498: E/GraphicsJNI(1291): VM won't let us allocate 1366400 bytes
09-02 08:22:49.208: E/AndroidRuntime(1291): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
有任何人對我的問題的解決方案或替代方法? 我讀了一些關於BitmapFactory的內容。它有幫助嗎? 最好的問候!
編輯:這裏是我的代碼: 佈局文件main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ViewAnimator
android:id="@+id/viewAnimator"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/LayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="@drawable/background"
...some Buttons etc. ...
</LinearLayout>
<LinearLayout
android:id="@+id/layoutGameMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="@drawable/gameMenu"
...some Buttons etc. ...
</LinearLayout>
<LinearLayout
android:id="@+id/layoutHighscores"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/highscores"
...some ListViews etc. ...
</LinearLayout>
</ViewAnimator>
</LinearLayout>
而且在MainActivity:
private ViewAnimator va;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
va = (ViewAnimator) findViewById(R.id.viewAnimator);
....
}
....
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if (va.getCurrentView() == findViewById(R.id.layoutHighscores)) {
va.setInAnimation(inFromRight);
va.setOutAnimation(outToLeft);
va.setDisplayedChild(0);
return true;
}
if (va.getCurrentView() == findViewById(R.id.layoutGameMenu)) {
va.setInAnimation(inFromLeft);
va.setOutAnimation(outToRight);
va.setDisplayedChild(0);
return true;
}
}
return super.onKeyDown(keyCode, event);
}
有了這個,我可以的MainMenu之間切換時,高分和帶按鈕的遊戲菜單來啓動遊戲模式。我選擇這個是因爲我想要「酷」的視圖動畫。
我試圖用BitmapFactory方法解決它,但沒有改變。
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);
}
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) {
if (width > height) {
inSampleSize = Math.round((float)height/(float)reqHeight);
} else {
inSampleSize = Math.round((float)width/(float)reqWidth);
}
}
return inSampleSize;
}
你在真實設備上試過了嗎? – wtsang02
不,我沒有這種尺寸的設備。這就是爲什麼我需要在模擬器上嘗試它。 – Namenlos