簡體   English   中英

內存不足錯誤需要解決方案

[英]OutOfMemory Error need solution

我知道使用位圖是一個常見的問題,但是我不知道如何通過示例進行操作。 尤其是因為圖像視圖的大小不大,您可能會說它會導致內存錯誤。 我認為這是因為我經常創建位圖,而不是一次在螞蟻每五秒鍾顯示一次,但是不知道該怎么做。 首先,用於創建位圖的代碼。

java類trapViews

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int x = 20;
    Random r = new Random();
    int i1 = r.nextInt(900 - 200) + 200;
    rnd = new Random();
    //Linke Seite

    System.gc();

    Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.stachelnstart);
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(image, i1, 300, true);
        float left = (float) 0;
        float top = (float) (getHeight() - resizedBitmap.getHeight());
        canvas.drawBitmap(resizedBitmap, left, top, paint);

        //rechte Seite
        Bitmap images = BitmapFactory.decodeResource(getResources(), R.drawable.stachelnstart1);
        Bitmap resizedBitmaps = Bitmap.createScaledBitmap(images, getWidth()-resizedBitmap.getWidth()-OwlHole, 300, true);
        float left1 = (float) (getWidth() - resizedBitmaps.getWidth());
        float top1 = (float) (getHeight() - resizedBitmaps.getHeight());
        canvas.drawBitmap(resizedBitmaps, left1, top1, paint);
    }
}

在屏幕的右側和左側創建具有隨機長度的可繪制對象。

現在,我通過HandlerMainActivity每5秒調用一次:

final Handler h = new Handler();
Runnable r = new Runnable() {
    public void run() {
        System.gc();
        traps();
        h.postDelayed(this,5000); // Handler neustarten
    }
}

private void traps() {
    container = (ViewGroup) findViewById(R.id.container);
    trapViews tv = new trapViews(this);
    container.addView(tv,
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT);
    //tV.setImageCount(8);
    h.postDelayed(r, 5000);
}

首先,它的工作方式就像我想要的那樣。 但是每次出現一個新的可繪制對象時,我的游戲就會停滯,經過5-6次創建后,它崩潰了

System.gc()bitmap.recycle函數不能很好地工作。 有人有解決方案嗎?

記住在替換位圖之后或不再可見時調用Bitmap.recycle()。 創建多個位圖不是問題,但是可以保留未使用的對象,而無需利用回收來釋放內存。

您每5秒創建一次位圖,這不是一個好主意,因為它們總是相同的。 您應該一次創建一次

 trapViews extends View{
    Bitmap image;
        Bitmap resizedBitmap;


        //rechte Seite
       Bitmap images ;
        Bitmap resizedBitmaps;

trapViews(Context c){
image = BitmapFactory.decodeResource(getResources(), R.drawable.stachelnstart);
images = BitmapFactory.decodeResource(getResources(), R.drawable.stachelnstart1);

}

@Override
protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int x = 20;
        Random r = new Random();
        int i1 = r.nextInt(900 - 200) + 200;
        rnd = new Random();
        //Linke Seite
          //I have left this bitmap in the here as it is affected by the random int
         resizedBitmap = Bitmap.createScaledBitmap(image, i1, 300, true);
            float left = (float) 0;
            float top = (float) (getHeight() - resizedBitmap.getHeight());
            canvas.drawBitmap(resizedBitmap, left, top, paint);

          //create this bitmap here as getWidth() will return 0 if created in the view's constructor
        if(resizedBitmaps == null)
         resizedBitmaps = Bitmap.createScaledBitmap(images, getWidth()-resizedBitmap.getWidth()-OwlHole, 300, true);
            float left1 = (float) (getWidth() - resizedBitmaps.getWidth());
            float top1 = (float) (getHeight() - resizedBitmaps.getHeight());
            canvas.drawBitmap(resizedBitmaps, left1, top1, paint);

}


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM