Created
June 27, 2013 09:55
-
-
Save jerieljan/5875323 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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