我正在遊戲,我想加載背景圖像canvas.ie背景應加載從隨機陣列和應該different.This是,我使用加載圖像隨機在畫布
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)
在哪種方法是執行上述代碼?在'globalBitmap'定義的範圍內? –
請發佈更多詳細信息代碼。 –