3

情况是这样的:我有一个具有三种不同布局的 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;
}
4

1 回答 1

0

在 Android 主站点的Android 培训部分中有一堂课涵盖了这种情况。本课“高效显示位图”分为四个部分,包括工作示例,并支持 API 级别 7 及更高级别(我认为这不是问题。)

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

于 2012-09-02T13:57:38.067 回答