我想你可以使用這塊內存緩存裝飾:
public class CustomMemoryCache implements MemoryCacheAware<String, Bitmap> {
private final MemoryCacheAware<String, Bitmap> cache;
public CustomMemoryCache(MemoryCacheAware<String, Bitmap> cache) {
this.cache = cache;
}
@Override
public boolean put(String key, Bitmap value) {
return cache.put(cleanKey(key), value);
}
@Override
public Bitmap get(String key) {
return cache.get(cleanKey(key));
}
@Override
public void remove(String key) {
cache.remove(cleanKey(key));
}
@Override
public Collection<String> keys() {
return cache.keys();
}
@Override
public void clear() {
cache.clear();
}
private String cleanKey(String key) {
return key.substring(0, key.lastIndexOf("?")) +
key.substring(key.lastIndexOf("_"));
// original cache key is like "[imageUri]_[width]x[height]"
}
}
然後換任何現成的內存緩存實現,並將其設置到配置。例如:
LruMemoryCache memoryCache = new LruMemoryCache(memoryCacheSize);
CustomMemoryCache memoryCacheDecorator = new CustomMemoryCache(memoryCache);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
...
.memoryCache(memoryCacheDecorator)
...
.build();