1

I have a ListView which shows products. Each product is having product details and a ImageView, but the problem I am doing lazy loading for the images of the product. And the images are high resolution. While scrolling it becomes clamsy (not smooth). Coz downloading the image takes time. Same way facebook have their images but that scrolling is much more smoother any solution please help.

kenju
  • 5,866
  • 1
  • 41
  • 41
DropAndTrap
  • 1,538
  • 16
  • 24

2 Answers2

1
public class BitmapCacheManager {
    private static LruCache<Object, Bitmap> cache = null;
    private final Context context;
    private static final int KB = 1024;
    private final Drawable placeHolder;


    public BitmapCacheManager(Context context) {
        this.context = context;
        placeHolder = context.getResources().getDrawable(R.drawable.unknown);
        int maxMemory = (int) (Runtime.getRuntime().maxMemory() / KB);
        int cacheSize = maxMemory / 7;
        cache = new LruCache<Object, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(Object albumId, Bitmap bitmap) {
                return (bitmap.getRowBytes() * bitmap.getHeight() / KB);
            }

            protected void entryRemoved(boolean evicted, Object key, Bitmap oldValue, Bitmap newValue) {
                oldValue.recycle();
            }
        };
    }

    public void addBitmapToMemoryCache(Object key, Bitmap bitmap) {
        if (bitmap != null && key != null && cache.get(key) == null)
            cache.put(key, bitmap);
    }

    public Bitmap getBitmapFromMemCache(Object key) {
        return cache.get(key);
    }

    public void loadBitmap(final Object key, final ImageView imageView) {
        final Bitmap bitmap = getBitmapFromMemCache(key);
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        } else {
            imageView.setImageDrawable(placeHolder);
            BitmapWorker task = new BitmapWorker(imageView);
            task.execute(key);
        }
    }

    private class BitmapWorker extends AsyncTask<Object, Void, Bitmap> {
        private final ImageView imageView;
        private Object key;

        public BitmapWorker(final ImageView imageView) {
            this.imageView = imageView;
        }

        @Implement
        protected Bitmap doInBackground(Object... params) {
            key = params[0];
            final Bitmap b = SomeClass.GetSomeBitmap(context, key);
            addBitmapToMemoryCache(key, b);
            return b;
        }

        @Override
        protected void onPostExecute(final Bitmap bitmap) {
            if (bitmap == null) {
                imageView.setImageBitmap(SomeClass.DefaultBitmap);
                return;
            }
            if (imageView.getTag().toString().equalsIgnoreCase(key.toString()) && !bitmap.isRecycled())
                imageView.setImageBitmap(bitmap);
        }
    }

}

and call:

bitmapCacheManager.loadBitmap(somekey, someImageView);
Autocrab
  • 3,474
  • 1
  • 15
  • 15
1
  1. Add Dummy images to all place holders, so scroll works smoothly.
  2. Fetch images based on need using Async task and replace dummy image with correct images once after image is ready.
  3. Cache the image with proper naming convention and choose cache size properly based on your image size.