Skip to content

Instantly share code, notes, and snippets.

@VassilisPallas
Created May 9, 2016 18:49
Show Gist options
  • Save VassilisPallas/2235f4e8393db7c8882e8b791d4b55d6 to your computer and use it in GitHub Desktop.
Save VassilisPallas/2235f4e8393db7c8882e8b791d4b55d6 to your computer and use it in GitHub Desktop.
LruCache for Volley
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import com.android.volley.toolbox.ImageLoader;
/**
* Created by vspallas on 09/02/16.
*/
public class LruBitmapCache extends LruCache<String, Bitmap> implements
ImageLoader.ImageCache {
public static int getDefaultLruCacheSize() {
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
return cacheSize;
}
public LruBitmapCache() {
this(getDefaultLruCacheSize());
}
public LruBitmapCache(int sizeInKiloBytes) {
super(sizeInKiloBytes);
}
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight() / 1024;
}
@Override
public Bitmap getBitmap(String url) {
return get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment