Android ViewAnimator和OutOfMemoryError

Android ViewAnimator和OutOfMemoryError,android,out-of-memory,viewanimator,Android,Out Of Memory,Viewanimator,情况就是这样:我有一个ViewAnimator,它有三种不同的布局。每个布局都有不同的背景图像。对于xhdpi设备,图像大小为1280x800像素,大小约为150KB。 现在的问题是:当我以1280x800的大小和320dp的密度启动emulator时,我总是收到以下异常: 09-02 08:22:46.408: D/dalvikvm(1291): GC_EXTERNAL_ALLOC freed 428K, 51% free 2874K/5831K, external 18484K/20407K

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

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>
有了它,我可以在主菜单、高分菜单和游戏菜单之间切换,并通过按钮启动游戏模式。我选择这个是因为我想要“酷”的视图动画

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

在安卓主站点的安卓培训部分有一节课介绍了这种情况。本课程分为四个部分,包括工作示例,并支持API级别7及以上(我认为这不是问题)


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

你试过真正的设备吗?没有,我没有这么大的设备。这就是为什么我需要在模拟器上试用它的原因。您好,我已经用BitmapFactory.decode*方法试用过,如所示,但没有效果。我仍然收到例外情况。如果您希望我可以发布一些代码,但我不相信这会有帮助,因为我只在布局中使用:android:background=“@drawable/background”设置背景图像。最好重新添加。@Namenlos,总是值得添加代码IMHO。如果没有别的,它会得到更多的关注。如果您担心显示太多代码(这似乎是一个常见问题),那么我建议您模拟一个示例。只需测试它,以确保它给出相同的错误;-)你是对的。我添加了一些代码。正如您所看到的,我已经用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;
}