我正在为小孩子开发一个游戏,其中包含不同的教育练习。在其中一个中,我需要一个图片背景,所以我正在加载它并使用 Matrix 对其进行缩放以填充屏幕。但有时加载图片会导致崩溃。
public DrawThread(SurfaceHolder surfaceHolder, Resources resources, float screen_x_max, float screen_y_max){
this.surfaceHolder = surfaceHolder;
screenWidth=screen_x_max;
screenHeight=screen_y_max;
// picture for bg
picture = BitmapFactory.decodeResource(resources, R.drawable.background);
...
}
在这个线程结束时,我回收它并为空。
if (this.picture!=null)
{
this.picture.recycle();
this.picture=null;
}
但应用程序仍然崩溃。有错误日志:
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:494)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:370)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:393)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:419)
at lexu.me.childstudy_lite.DrawThreadFindAnimals2.<init>(findAnimals2.java:271)
at lexu.me.childstudy_lite.findanimals2_view.surfaceCreated(findAnimals2.java:186)
at android.view.SurfaceView.updateWindow(SurfaceView.java:548)
at android.view.SurfaceView.dispatchDraw(SurfaceView.java:353)
at android.view.ViewGroup.drawChild(ViewGroup.java:1737)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1466)
at android.view.ViewGroup.drawChild(ViewGroup.java:1737)
谢谢。