I read some of the sample using LruCache to implement a cache mechanism for storing the bitmap image. But I still don't know how to use it even through I have read the documenthttp://developer.android.com/reference/android/util/LruCache.html for it.
For example, in document, it mentioned "Returns the size of the entry for key and value in user-defined units." in sizeof(). What is the size of entry mean? is it mean the number of entries it allow, e.g return 10 would allow me to have 10 cache object references.android
public class LruBitmapCache extends LruCache<String, Bitmap> implements 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 getByteCount / 1024; ...
In the above code, why it need to divide 1024, what is the propose for it?
Also, the constructor LruBitmapCache(int sizeInKiloBytes), why the parameter claim it is size in kilobytes? isn't it should be size in bytes according to the document above?
Any help would be appreciated, thanks! I am confusing...app
maxSize
andsizeOf
are the same, everything goes correctly. But in your explanation on the second option for the case when i care about the total size of sum of all elements, I have still some confusion. In your code,sizeOf
returnvalue.length()
. Is it main that if I have 4 items in theLruCache
, their size are different, their are 100,200,300,400. Then, the total is 1000. Given that I setmaxSize
to 100000, e.gLruCache<>(100000)
. Then, all 4 items are able to store in the cache, right? – bufferoverflow76 Sep 1 '14 at 16:39