package com.cocoahero.android.essentials.graphics;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

/**
 * This class is a specialized implementation of {@link android.util.LruCache}
 * designed for caching {@link Bitmap}s.
 * 
 * Instead of limiting the number of cached objects, it limits based on the
 * amount of memory used by the cached bitmaps.
 */
public class BitmapCache extends LruCache<String, Bitmap> {

    /**
     * Constructs a new {@link BitmapCache} allowing you to specify the maximum
     * amount of memory used by the cache.
     * 
     * @param bytes The maximum amount of memory in bytes this cache should use.
     */
    public BitmapCache(int bytes) {
        super(bytes);
    }

    /**
     * {@inheritDoc android.util.LruCache}
     */
    @Override
    protected int sizeOf(String key, Bitmap bitmap) {
        return (bitmap.getRowBytes() * bitmap.getHeight());
    }

}

@@ -45,16 +45,84 @@
 phics;%0A%0A
+import android.app.ActivityManager;%0Aimport android.content.Context;%0A
 import a
@@ -504,28 +504,22 @@
 map%3E %7B%0A%0A
-    /**%0A    
+%09/**%0A%09
  * Const
@@ -586,20 +586,17 @@
 maximum%0A
-    
+%09
  * amoun
@@ -630,20 +630,14 @@
 he.%0A
-     * %0A    
+%09 * %0A%09
  * @
@@ -709,28 +709,22 @@
 ld use.%0A
-     */%0A    
+%09 */%0A%09
 public B
@@ -747,24 +747,18 @@
 ytes) %7B%0A
-        
+%09%09
 super(by
@@ -767,27 +767,18 @@
 s);%0A
-    %7D%0A%0A    /**%0A    
+%09%7D%0A%0A%09/**%0A%09
  * %7B
@@ -816,20 +816,14 @@
 he%7D%0A
-     */%0A    
+%09 */%0A%09
 @Ove
@@ -828,20 +828,17 @@
 verride%0A
-    
+%09
 protecte
@@ -883,72 +883,1281 @@
 ) %7B%0A
-        return (bitmap.getRowBytes() * bitmap.getHeight());%0A    
+%09%09return (bitmap.getRowBytes() * bitmap.getHeight());%0A%09%7D%0A%0A%09/**%0A%09 * A factory for creating a %7B@link BitmapCache%7D based on the application's%0A%09 * memory class.%0A%09 */%0A%09public static class BitmapCacheFactory %7B%0A%0A%09%09/**%0A%09%09 * Returns a new %7B@link BitmapCache%7D instance configured to use 1/8th of%0A%09%09 * the application's memory class.%0A%09%09 * %0A%09%09 * @param context%0A%09%09 * @return a new %7B@link BitmapCache%7D instance configured to use 1/8th of%0A%09%09 *         the application's memory class.%0A%09%09 */%0A%09%09public static BitmapCache getCache(Context context) %7B%0A%09%09%09return getCache(context, 0.125f);%0A%09%09%7D%0A%0A%09%09/**%0A%09%09 * Returns a new %7B@link BitmapCache%7D instance configured to use the%0A%09%09 * specified percentage of the application's memory class.%0A%09%09 * %0A%09%09 * @param context%0A%09%09 * @param percentage A floating value between 1.0f and 0.0f.%0A%09%09 * @return a new %7B@link BitmapCache%7D instance configured to use the%0A%09%09 *         given percentage of the application's memory class.%0A%09%09 */%0A%09%09public static BitmapCache getCache(Context context, float percentage) %7B%0A%09%09%09ActivityManager mgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);%0A%0A%09%09%09final int memoryClass = mgr.getMemoryClass();%0A%09%09%09final int cacheSize = (int) (1048576 * memoryClass * percentage);%0A%0A%09%09%09return new BitmapCache(cacheSize);%0A%09%09%7D%0A%0A%09
 %7D%0A%0A%7D

Add factory for creating BitmapCache instances based on memory class