0

I am trying to implement Lru Cache into my program to have my listview scroll faster. I am not sure what to place in the Bitmap value of addBitmapToCache(String key, Bitmap bitmap (indicated by XXXXXX). Anyone have a suggestion? Thank you.

public class ImageCache extends LruCache<String, Bitmap> {

public ImageCache(int maxSize) {
    super(maxSize);
}

@Override
protected int sizeOf(String key, Bitmap value) {
    return value.getByteCount() / 1024;
}

public void addBitmapToCache(String key, Bitmap bitmap) {
    if (getBitmapFromCache(key) == null) {
        put(key, bitmap);
    }
}

public Bitmap getBitmapFromCache(String key) {
    return get(key);
}
}

And here is where I implement it.

@Override
public View getView(int position, View convertView, ViewGroup parent)
{

    ViewHolder holder;
    View rowView = convertView;

    if(rowView==null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.listview_layout, parent, false);
        holder = new ViewHolder();
        holder.img = (ImageView) rowView.findViewById(R.id.flag);
        rowView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) rowView.getTag();
    }
    //holder.img.setImageResource(imageIds.get(position));

    loadBitmap(imageIds.get(position),holder.img);
    return rowView;
}

public void loadBitmap(int resId, ImageView imageView) {
    final String imageKey = String.valueOf(resId);
    Bitmap storeBitmap;
    final Bitmap bitmap = mCache.getBitmapFromCache(imageKey);
    if (bitmap != null) {
        imageView.setImageBitmap(bitmap);
    } else {
        imageView.setImageResource(resId);
        storeBitmap = BitmapFactory.decodeResource(imageView.getResources(), resId);
        mCache.addBitmapToCache(String.valueOf(resId), storeBitmap);
    }
}
Michael
  • 83
  • 9

1 Answers1

0

Call BitmapFactory.decodeResource(imageView.getResources(), resId) to load your bitmap from resources and put it in your image cache. Next time you'll need it, it will be present in cache which will speed up setup of list item view.

michal.z
  • 2,025
  • 1
  • 15
  • 10
  • I updated loadBitmap in my post with your changes. I never enter the if statement, only ever enters the else. mBitmap is always null. Why does it not store into my cache correctly? Thanks – Michael Aug 25 '14 at 01:44
  • Check how big is size of your cache. Maybe it's too small and it's elements are removed before you want to reused them. Verify also whether bitmaps loaded through BitmapFactory.decodeResource() are loaded correctly. Maybe your resIds are incorrect? – michal.z Aug 25 '14 at 08:22
  • You should also use `imageView.setImageBitmap()` instead of `imageView.setImageResource()` in else statement. Currently you perform disk operation while loading bitmap from resource twice - in `imageView.setImageResource()` and in `BitmapFactory.decodeResource()`. In your cache `sizeOf()` method you use `getByteCount()` which is available since API level 12. If you want to support earlier API versions you can check bitmap size like so: – michal.z Aug 25 '14 at 09:12
  • `int getBitmapByteSize(Bitmap bitmap) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { return bitmap.getAllocationByteCount(); } else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1){ return bitmap.getByteCount(); } else { return bitmap.getRowBytes() * bitmap.getHeight(); } }` – michal.z Aug 25 '14 at 09:13