Android custom view Bitmap memory leak

前端 未结 2 1025
我在风中等你
我在风中等你 2020-12-05 01:09

I\'ve got a custom view in which I need to draw two bitmaps, one is a background, representing the image of a map and one is a pin which will be drawn on top/left position i

2条回答
  •  [愿得一人]
    2020-12-05 01:25

    Something that marked me in your code is that you don't recycle all your bitmaps apparently. Here are a couple of code snippets that could help:

    bmp = getBitmapFromRessourceID(R.drawable.menu);
    ratio = (float)this.height/(float)bmp.getScaledHeight(canvas);
    temp = Bitmap.createScaledBitmap(bmp, (int)(bmp.getWidth()*ratio), (int) (bmp.getHeight()*ratio-10), false);
    bmp.recycle();
    bmp = temp;
    

    and:

    Bitmap temp = BitmapFactory.decodeResource(context.getResources(), resource, opts);
    Bitmap bmp = Bitmap.createBitmap(temp, 0, 0, temp.getWidth(), temp.getHeight(), flip, true);
    temp.recycle();
    

提交回复
热议问题