Created
May 9, 2016 18:49
-
-
Save VassilisPallas/2235f4e8393db7c8882e8b791d4b55d6 to your computer and use it in GitHub Desktop.
LruCache for Volley
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
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