Android ViewAnimator和OutOfMemoryError

时间:2012-09-02 08:39:51

标签: android out-of-memory viewanimator

情况就是这样:我有一个具有三种不同布局的ViewAnimator。每个布局都有不同的背景图像。对于xhdpi设备,图像大小为1280x800像素,大小约为150KB。 现在的问题是:当我启动大小为1280x800且密度为320dp的模拟器时,我总是会收到此异常:

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);
}

有了这个,我可以在主菜单,高分和游戏菜单之​​间切换按钮来启动游戏模式。我选择这个是因为我想要“酷”的视图动画。

我尝试使用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;
}

1 个答案:

答案 0 :(得分:0)

有一节课涵盖了Android主网站的 Android Training 部分中的这种情况。课程Displaying Bitmaps Efficiently分为四个部分,包括工作示例,并支持API级别7及以上(我认为这不是问题。)

如果这没有用,我建议发布一些代码,以便提供更具体的帮助。 ; - )