OSDN Git Service

Bitmapfun Sample: Minor updates/fixes.
authorAdam Koch <akoch@google.com>
Thu, 7 Nov 2013 22:07:04 +0000 (17:07 -0500)
committerAdam Koch <akoch@google.com>
Thu, 7 Nov 2013 22:08:08 +0000 (22:08 +0000)
Change-Id: I6ac19a95a65ab5210f62d1f50d76f2b3e1b533ef

docs/downloads/training/BitmapFun.zip
docs/html/training/displaying-bitmaps/manage-memory.jd

index 8668897..d3dd02a 100644 (file)
Binary files a/docs/downloads/training/BitmapFun.zip and b/docs/downloads/training/BitmapFun.zip differ
index 0e1279e..7f2b4c5 100644 (file)
@@ -160,13 +160,14 @@ a soft reference to the bitmap is placed
 in a {@link java.util.HashSet}, for possible reuse later with
 {@link android.graphics.BitmapFactory.Options#inBitmap}:
 
-<pre>HashSet&lt;SoftReference&lt;Bitmap&gt;&gt; mReusableBitmaps;
+<pre>Set&lt;SoftReference&lt;Bitmap&gt;&gt; mReusableBitmaps;
 private LruCache&lt;String, BitmapDrawable&gt; mMemoryCache;
 
-// If you're running on Honeycomb or newer, create
-// a HashSet of references to reusable bitmaps.
+// If you're running on Honeycomb or newer, create a
+// synchronized HashSet of references to reusable bitmaps.
 if (Utils.hasHoneycomb()) {
-    mReusableBitmaps = new HashSet&lt;SoftReference&lt;Bitmap&gt;&gt;();
+    mReusableBitmaps =
+            Collections.synchronizedSet(new HashSet&lt;SoftReference&lt;Bitmap&gt;&gt;());
 }
 
 mMemoryCache = new LruCache&lt;String, BitmapDrawable&gt;(mCacheParams.memCacheSize) {
@@ -243,25 +244,27 @@ protected Bitmap getBitmapFromReusableSet(BitmapFactory.Options options) {
         Bitmap bitmap = null;
 
     if (mReusableBitmaps != null && !mReusableBitmaps.isEmpty()) {
-        final Iterator&lt;SoftReference&lt;Bitmap&gt;&gt; iterator
-                = mReusableBitmaps.iterator();
-        Bitmap item;
-
-        while (iterator.hasNext()) {
-            item = iterator.next().get();
-
-            if (null != item && item.isMutable()) {
-                // Check to see it the item can be used for inBitmap.
-                if (canUseForInBitmap(item, options)) {
-                    bitmap = item;
-
-                    // Remove from reusable set so it can't be used again.
+        synchronized (mReusableBitmaps) {
+            final Iterator&lt;SoftReference&lt;Bitmap&gt;&gt; iterator
+                    = mReusableBitmaps.iterator();
+            Bitmap item;
+
+            while (iterator.hasNext()) {
+                item = iterator.next().get();
+
+                if (null != item && item.isMutable()) {
+                    // Check to see it the item can be used for inBitmap.
+                    if (canUseForInBitmap(item, options)) {
+                        bitmap = item;
+
+                        // Remove from reusable set so it can't be used again.
+                        iterator.remove();
+                        break;
+                    }
+                } else {
+                    // Remove from the set if the reference has been cleared.
                     iterator.remove();
-                    break;
                 }
-            } else {
-                // Remove from the set if the reference has been cleared.
-                iterator.remove();
             }
         }
     }