Skip to content

Instantly share code, notes, and snippets.

@jerieljan
Created June 27, 2013 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jerieljan/5875323 to your computer and use it in GitHub Desktop.
Save jerieljan/5875323 to your computer and use it in GitHub Desktop.
package ph.com.voyager.epg.core.model.volley;
import com.android.volley.toolbox.ImageLoader;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
/**
* A straightforward implementation of the {@link ImageLoader.ImageCache ImageCache} in LruCache
* provided by Android + Support Library.
*
* Originally written by Ficus Kirkpatrick: https://gist.github.com/ficusk/5614325
*
* @author jerieljan
*/
public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache {
/**
* Creates a new BitmapLruCache with the default cache size.
*/
public BitmapLruCache() {
this(getDefaultLruCacheSize());
}
/**
* Creates a new BitmapLruCache with a defined amount of KB on memory.
*/
public BitmapLruCache(int sizeInKiloBytes) {
super(sizeInKiloBytes);
}
/**
* Retrieves the default LruCache size.
*
* The value is based on 1/8 of the devices' maximum memory.
*/
public static int getDefaultLruCacheSize() {
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
return cacheSize;
}
@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