有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java OutOfMemory错误需要解决

我知道使用位图是一个常见的问题,但我不知道如何使用这个示例。特别是因为图像视图的大小不大,所以您会说它应该会导致内存错误。我想这是因为我经常创建位图,而不是创建一次,每五秒钟显示一次,但不知道怎么做。首先是创建位图的代码

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);
    }
}

在屏幕的右侧和左侧创建具有随机长度的可绘制图形

现在我在MainActivity中每隔5秒用Handler调用一次:

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函数工作得不是很好。有人有解决办法吗


共 (2) 个答案

  1. # 1 楼答案

    您每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);
    
    }
    
    
    }
    
  2. # 2 楼答案

    记得调用位图。替换位图后或位图不再可见时回收()。 创建多个位图不是问题,而是在不使用回收来释放内存的情况下保留未使用的对象