开发者

Android Bitmaps inside ListView -> out of Memory

开发者 https://www.devze.com 2023-04-03 09:19 出处:网络
I have a ListView where each item can have up to 10 icons. If I scro开发者_如何学JAVAll the ListView up and down, I get a force-close, saying Bitmap size exceeds VM Budget, and that I\'m out of Memory

I have a ListView where each item can have up to 10 icons. If I scro开发者_如何学JAVAll the ListView up and down, I get a force-close, saying Bitmap size exceeds VM Budget, and that I'm out of Memory. I read here about recycling Bitmaps, so I write a Method, which recycles my Bitmaps. Problem is that the scrolling is f... slow. Here is my Function:

 private void recycleBmpsFromConvertView(View convertView){
    if (convertView != null && convertView instanceof LinearLayout) {
        LinearLayout iconArea = (LinearLayout)convertView;
        for (int i = 0; i < iconArea.getChildCount(); i++) {
            View v = iconArea.getChildAt(i);
            if (v instanceof LinearLayout) {
                LinearLayout row = (LinearLayout) v;
                for (int j = 0; j < row.getChildCount(); j++) {
                    View candidate = row.getChildAt(j);
                    if (candidate instanceof ImageView) {
                        ImageView image = (ImageView) candidate;
                        Drawable d = image.getDrawable();
                        Bitmap bmp = null;
                        if (d instanceof SvgDrawable) {
                            SvgDrawable svg = (SvgDrawable) d;
                            bmp = svg.getBitmap();
                        } else if (d instanceof BitmapDrawable) {
                            BitmapDrawable bmpd = (BitmapDrawable) d;
                            bmp = bmpd.getBitmap();
                        }
                        if (bmp != null) {
                            bmp.recycle();
                            bmp = null;
                        }
                    }
                }
            }
        }
    }
    System.gc();
}

I try to delete the Bitmaps, as soon as my convertView is not null. Is there a faster Way for my approach?


Remove recycling

  1. Put a lower resolution icon or
  2. downsample, if you are decoding the image before setting it . This can be done by using insample setting before bitmap decode.
0

上一篇:

:下一篇

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号