Created
August 18, 2012 13:49
-
-
Save kojiokb/3386953 to your computer and use it in GitHub Desktop.
LruCacheの使い方
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; | |
public final class ImageCache { | |
private static final int MEM_CACHE_SIZE = 1 * 1024 * 1024; // 1MB | |
private static LruCache<String, Bitmap> sLruCache; | |
static { | |
sLruCache = new LruCache<String, Bitmap>(MEM_CACHE_SIZE) { | |
@Override | |
protected int sizeOf(String key, Bitmap bitmap) { | |
return bitmap.getRowBytes() * bitmap.getHeight(); | |
} | |
}; | |
} | |
private ImageCache() { | |
} | |
public static void setImage(String key, Bitmap bitmap) { | |
if (getImage(key) == null) { | |
sLruCache.put(key, bitmap); | |
} | |
} | |
public static Bitmap getImage(String key) { | |
return sLruCache.get(key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment