2014-02-14 147 views
1

我正在遊戲,我想加載背景圖像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) 
+1

在哪種方法是執行上述代碼?在'globalBitmap'定義的範圍內? –

+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(); 
    } 
+0

我認爲你的方法工作。但我得到內存錯誤。所以我不能確定它是否正常工作。因爲一旦應用程序強制退出,我再次加載應用程序我得到不同的background.so有沒有辦法擺脫內存不足的錯誤。我首先使用了png圖像,現在jpeg仍然出現該錯誤。 –

+0

我認爲你的圖片太大了,試圖調整圖片大小,並在發佈之前回收()你的位圖 –

+0

我應該在哪裏使用bitmap.recycle(); ? –

0

你得到一個OutOfMemoryError這意味着你正在運行你的應用程序中沒有按環境沒有足夠的內存可用於完全加載您嘗試加載的位圖。

解決方法是加載圖像的縮小版本,使其適合內存。所有的細節如何做到這一點,給出並解釋here,看看它,並重用他們的decodeSampledBitmapFromResource()方法,而不是隻調用Resources#getDrawable()。 請注意,除非您完全確定原始圖像的大小,否則應始終加載此類圖像。

然後,還有一個問題,就是你不知道第一手有多少內存可用,所以你不知道reqWidthreqHeight這將使你可以確保沒有out的內存錯誤發生。這就是爲什麼我通常會打電話來解碼圖像的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; 
    } 
} 
+0

嗨,那麼你能告訴我如何修改上面提到的代碼嗎?即我需要從陣列加載圖像到畫布,它不應該有內存錯誤 –

+0

是的,這是我只是想回答你的問題。嘗試閱讀並理解它的含義(並且不要忘記對鏈接的文章也做同樣的事情),你應該看看如何修改你給定的代碼。我很高興進一步解釋一些不清楚或者你不明白的東西,但是我不能爲你寫代碼;這是唯一一個「解釋我」問答網站,不是「爲我做」的自由職業者派遣網站:) – desseim