Hendrx Hendrx - 11 days ago 8
Android Question

OutOfMemory Error need solution

i know its a common problem for working with bitmaps but i dont know how to go on on this example. Escpessially because the size of the image view is not this big that you would say it should cause a memory error. I think its because i create the bitmaps to often instead of creating it once ant display it every five seconds but dont know how to do it ..
first of all the code for creating the bitmaps.

bitmaps
java class 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);

}
}


creates a drawable on the right and left side of the screen with an random length

now i call this every 5 sec in the MainActivity with an 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);
}


first of alle its working like i want it to work. But everytime a new drawable appears my game is lagging and after 5-6 times creating one its crashing down

the System.gc() and bitmap.recycle functions arent working really well

has anybody a solution??

and sorry for bad english im german^^

Greetings Hendrix :)

Answer

You're creating the bitmaps every 5 seconds which is not a good idea as they are always the same. You should create them once instead

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

}


}