If you think the Android project cube-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package in.srain.cube.image.impl;
/*fromwww.java2s.com*/import android.graphics.drawable.BitmapDrawable;
import android.support.v4.util.LruCache;
import android.util.Log;
import in.srain.cube.image.ImageProvider;
import in.srain.cube.image.drawable.RecyclingBitmapDrawable;
import in.srain.cube.image.iface.ImageMemoryCache;
import in.srain.cube.util.Debug;
publicclass DefaultMemoryCache implements ImageMemoryCache {
protectedstaticfinalboolean DEBUG = Debug.DEBUG_IMAGE;
protectedstaticfinal String LOG_TAG = Debug.DEBUG_IMAGE_LOG_TAG_PROVIDER;
private LruCache<String, BitmapDrawable> mMemoryCache;
privatestatic DefaultMemoryCache sDefault;
publicstatic DefaultMemoryCache getDefault() {
if (null == sDefault) {
int size = Math.round(0.2f * Runtime.getRuntime().maxMemory() / 1024);
sDefault = new DefaultMemoryCache(size);
}
return sDefault;
}
public DefaultMemoryCache(int cacheSizeInKB) {
// Set up memory cache
if (DEBUG) {
Log.d(LOG_TAG, "Memory cache created (size = " + cacheSizeInKB + " KB)");
}
mMemoryCache = new LruCache<String, BitmapDrawable>(cacheSizeInKB) {
/**
* Notify the removed entry that is no longer being cached
*/
@Override
protectedvoid entryRemoved(boolean evicted, String key, BitmapDrawable oldValue, BitmapDrawable newValue) {
if (RecyclingBitmapDrawable.class.isInstance(oldValue)) {
// The removed entry is a recycling drawable, so notify it
// that it has been removed from the memory cache
((RecyclingBitmapDrawable) oldValue).setIsCached(false);
} else {
// The removed entry is a standard BitmapDrawable
// do nothing
}
}
/**
* Measure item size in kilobytes rather than units which is more practical for a bitmap cache
*/
@Override
protectedint sizeOf(String key, BitmapDrawable value) {
finalint bitmapSize = ImageProvider.getBitmapSize(value) / 1024;
return bitmapSize == 0 ? 1 : bitmapSize;
}
};
}
@Override
publicvoid set(String key, BitmapDrawable drawable) {
if (key == null || drawable == null) {
return;
}
// Add to memory cache
if (mMemoryCache != null) {
if (RecyclingBitmapDrawable.class.isInstance(drawable)) {
// The removed entry is a recycling drawable, so notify it
// that it has been added into the memory cache
((RecyclingBitmapDrawable) drawable).setIsCached(true);
}
mMemoryCache.put(key, drawable);
}
}
/**
* Get from memory cache.
*/
@Override
public BitmapDrawable get(String key) {
BitmapDrawable memValue = null;
if (mMemoryCache != null) {
memValue = mMemoryCache.get(key);
}
return memValue;
}
/**
* clear the memory cache
*/
@Override
publicvoid clear() {
if (mMemoryCache != null) {
mMemoryCache.evictAll();
if (DEBUG) {
Log.d(LOG_TAG, "Memory cache cleared");
}
}
}
@Override
publicvoid delete(String key) {
mMemoryCache.remove(key);
}
@Override
publiclong getMaxSize() {
return mMemoryCache.maxSize() * 1024;
}
@Override
publiclong getUsedSpace() {
return mMemoryCache.size() * 1024;
}
}