package com.diandi.demo.io;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
/**
* *******************************************************************************
* ********* Author : klob(kloblic@gmail.com) .
* ********* Date : 2014-11-29 .
* ********* Time : 11:46 .
* ********* Project name : Diandi1.18 .
* ********* Version : 1.0
* ********* Copyright @ 2014, klob, All Rights Reserved
* *******************************************************************************
*/
public class MemoryCache {
private final LruCache<Integer, Bitmap> mMemoryCache;
public MemoryCache() {
final int cacheSize = (int) (Runtime.getRuntime().maxMemory() / 1024);
mMemoryCache = new LruCache<Integer, Bitmap>(cacheSize) {
@Override
protected int sizeOf(final Integer key, final Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
}
};
}
public void addBitmapToMemoryCache(final int key, final Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(final int key) {
return mMemoryCache.get(key);
}
}