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
- Put a lower resolution icon or
- downsample, if you are decoding the image before setting it . This can be done by using insample setting before bitmap decode.
精彩评论