我正在制作游戏,我想将背景图像加载到canvas.ie背景应该从数组加载随机而应该是不同的。这是我使用的代码
int[] wallpaper={R.drawable.bg,R.drawable.bg_two,R.drawable.bg_three,R.drawable.bg_four, };
if(globalBitmap == null){
Random random = new Random();
int n=random.nextInt(wallpaper.length);
BitmapDrawable bd = (BitmapDrawable) context.getResources().getDrawable(wallpaper[n]);
globalBitmap = bd.getBitmap();
}
this.bitmap = globalBitmap;
但是当我加载游戏时。只显示相同的背景。所有4个背景都不会以随机顺序加载。可以帮助解决这个问题吗?
logcat的
02-14 14:38:04.757: E/AndroidRuntime(10518): FATAL EXCEPTION: main
02-14 14:38:04.757: E/AndroidRuntime(10518): Process: com.themebowlapps.monkeyrun, PID: 10518
02-14 14:38:04.757: E/AndroidRuntime(10518): java.lang.OutOfMemoryError
02-14 14:38:04.757: E/AndroidRuntime(10518): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-14 14:38:04.757: E/AndroidRuntime(10518): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
02-14 14:38:04.757: E/AndroidRuntime(10518): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)
02-14 14:38:04.757: E/AndroidRuntime(10518): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872)
02-14 14:38:04.757: E/AndroidRuntime(10518): at android.content.res.Resources.loadDrawable(Resources.java:3022)
02-14 14:38:04.757: E/AndroidRuntime(10518): at android.content.res.Resources.getDrawable(Resources.java:1586)
02-14 14:38:04.757: E/AndroidRuntime(10518): at com.themebowlapps.monkeyrun.Background.getRandom(Background.java:37)
02-14 14:38:04.757: E/AndroidRuntime(10518): at com.themebowlapps.monkeyrun.Background.testMethod(Background.java:43)
02-14 14:38:04.757: E/AndroidRuntime(10518): at com.themebowlapps.monkeyrun.Background.<init>(Background.java:30)
02-14 14:38:04.757: E/AndroidRuntime(10518): at com.themebowlapps.monkeyrun.Frontground.<init>(Frontground.java:19)
02-14 14:38:04.757: E/AndroidRuntime(10518): at com.themebowlapps.monkeyrun.GameView.<init>(GameView.java:50)
02-14 14:38:04.757: E/AndroidRuntime(10518): at com.themebowlapps.monkeyrun.Game.onCreate(Game.java:41)
02-14 14:38:04.757: E/AndroidRuntime(10518): at android.app.Activity.performCreate(Activity.java:5389)
02-14 14:38:04.757: E/AndroidRuntime(10518): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-14 14:38:04.757: E/AndroidRuntime(10518): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
02-14 14:38:04.757: E/AndroidRuntime(10518): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
02-14 14:38:04.757: E/AndroidRuntime(10518): at android.app.ActivityThread.access$800(ActivityThread.java:157)
02-14 14:38:04.757: E/AndroidRuntime(10518): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
02-14 14:38:04.757: E/AndroidRuntime(10518): at android.os.Handler.dispatchMessage(Handler.java:102)
02-14 14:38:04.757: E/AndroidRuntime(10518): at android.os.Looper.loop(Looper.java:157)
02-14 14:38:04.757: E/AndroidRuntime(10518): at android.app.ActivityThread.main(ActivityThread.java:5293)
02-14 14:38:04.757: E/AndroidRuntime(10518): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 14:38:04.757: E/AndroidRuntime(10518): at java.lang.reflect.Method.invoke(Method.java:515)
02-14 14:38:04.757: E/AndroidRuntime(10518): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1259)
02-14 14:38:04.757: E/AndroidRuntime(10518): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
02-14 14:38:04.757: E/AndroidRuntime(10518): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
你的globalBitmap只加载一次......尝试这段代码,也许可以帮助你
int[] wallpaper={R.drawable.bg,R.drawable.bg_two,R.drawable.bg_three,R.drawable.bg_four, };
BitmapDrawable getRandom() {
Random random = new Random();
int n = random.nextInt(wallpaper.length);
BitmapDrawable bd = (BitmapDrawable) context.getResources().getDrawable(wallpaper[n]);
return bd.getBitmap();
}
void testMethod() {
this.bitmap = getRandom();
}
答案 1 :(得分:0)
你得到一个OutOfMemoryError
,这意味着您运行应用程序的环境没有足够的内存可用于完全加载您尝试加载的位图。
解决方案是加载图像的缩小版本,使其适合内存。所有详细信息如何执行并解释here,查看并重用他们的decodeSampledBitmapFromResource()
方法,而不是仅调用Resources#getDrawable()
。
请注意,除非您完全确定原始图像的大小,否则应始终加载这样的图像。
然后,还有一个问题是你不知道你有多少可用的内存,所以你无法知道reqWidth
和reqHeight
这将使你能够加载一个图像肯定没有内存不足错误发生。这就是为什么我通常将调用解包为try
块中的图像来捕获OutOfMemoryError
异常,然后循环它以重试加载较小版本的图像以防出错,直到找到可加载的大小。
这种味道的东西(免责声明:代码未经测试):
final int imageResourceId = ...; // do your random selection here
Bitmap loadedBitmap = null;
int reqWidth = DEFAULT_REQ_WIDTH, reqHeight = DEFAULT_REQ_HEIGHT;
while (loadedBitmap == null && reqWidth >= MIN_REQ_WIDTH && reqHeight >= MIN_REQ_HEIGHT) {
try {
loadedBitmap = decodeSampledBitmapFromResource(getResources(), imageResourceId, reqWidth, reqHeight);
} catch (final OutOfMemoryError oOM) {
// not enough memory to load an image this size, retry with a smaller one
reqWidth /= 2;
reqHeight /= 2;
}
}