[Android] Add an LRUCache to store the frequently used bitmaps

Ludovic Fauvet git at videolan.org
Mon Oct 15 19:16:55 CEST 2012


vlc-ports/android | branch: master | Ludovic Fauvet <etix at videolan.org> | Mon Oct 15 19:02:57 2012 +0200| [24f2b75f7b3e530184cc0640c2b3e331fa01ecd6] | committer: Ludovic Fauvet

Add an LRUCache to store the frequently used bitmaps

The cache can be used to store almost any kind of bitmaps but its main
purpose is to speedup the UI (like lists and grids) and because the
memory of the cache is limited and constrained by device your should
avoid storing bitmaps that are not going to be shown very often or it
would void the benefits from the system on low-memory devices.

If you think this sentence is very long, you never read Proust.

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=24f2b75f7b3e530184cc0640c2b3e331fa01ecd6
---

 vlc-android/src/org/videolan/vlc/BitmapCache.java |   91 +++++++++++++++++++++
 1 file changed, 91 insertions(+)

diff --git a/vlc-android/src/org/videolan/vlc/BitmapCache.java b/vlc-android/src/org/videolan/vlc/BitmapCache.java
new file mode 100644
index 0000000..5b21b3b
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/BitmapCache.java
@@ -0,0 +1,91 @@
+/*****************************************************************************
+ * BitmapCache.java
+ *****************************************************************************
+ * Copyright © 2012 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+package org.videolan.vlc;
+
+import android.app.ActivityManager;
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.support.v4.util.LruCache;
+import android.util.Log;
+
+public class BitmapCache {
+
+    public final static String TAG = "VLC/BitmapCache";
+    private final static boolean LOG_ENABLED = false;
+
+    private static BitmapCache mInstance;
+    private final LruCache<String, Bitmap> mMemCache;
+
+    public static BitmapCache getInstance() {
+        if (mInstance == null)
+            mInstance = new BitmapCache();
+        return mInstance;
+    }
+
+    private BitmapCache() {
+
+        // Get the global context of the application
+        Context context = VLCApplication.getAppContext();
+
+        // Get memory class of this device, exceeding this amount will throw an
+        // OutOfMemory exception.
+        final int memClass = ((ActivityManager) context.getSystemService(
+                Context.ACTIVITY_SERVICE)).getMemoryClass();
+
+        // Use 1/5th of the available memory for this memory cache.
+        final int cacheSize = 1024 * 1024 * memClass / 5;
+
+        Log.d(TAG, "LRUCache size sets to " + cacheSize);
+
+        mMemCache = new LruCache<String, Bitmap>(cacheSize) {
+
+            @Override
+            protected int sizeOf(String key, Bitmap value) {
+                return value.getRowBytes() * value.getHeight();
+            }
+
+        };
+    }
+
+    public Bitmap getBitmapFromMemCache(String key) {
+        final Bitmap b = mMemCache.get(key);
+        if (LOG_ENABLED) {
+            if (b == null)
+                Log.d(TAG, "Cache miss");
+            else
+                Log.d(TAG, "Cache found");
+        }
+        return mMemCache.get(key);
+    }
+
+    public void addBitmapToMemCache(String key, Bitmap bitmap) {
+        if (key != null && bitmap != null && getBitmapFromMemCache(key) == null)
+            mMemCache.put(key, bitmap);
+    }
+
+    public Bitmap getBitmapFromMemCache(int resId) {
+        return mMemCache.get("res:" + resId);
+    }
+
+    public void addBitmapToMemCache(int resId, Bitmap bitmap) {
+        addBitmapToMemCache("res:" + resId, bitmap);
+    }
+}



More information about the Android mailing list