From 9f9d0a59c4dab713b6cd8f5a29ea20f646bc5ac0 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Wed, 25 Feb 2015 11:34:17 -0800 Subject: Picking a bitmap to reuse where least pixels are wasted Change-Id: I4217bc68a5caa2d1526e4ebb101dbaf0348066d3 --- .../android/launcher3/WallpaperCropActivity.java | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'WallpaperPicker') diff --git a/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java b/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java index 0ddb79e4f..a3a3c537b 100644 --- a/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java +++ b/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java @@ -50,7 +50,6 @@ import com.android.photos.BitmapRegionTileSource.BitmapSource.InBitmapProvider; import com.android.photos.views.TiledImageRenderer.TileSource; import java.util.Collections; -import java.util.Iterator; import java.util.Set; import java.util.WeakHashMap; @@ -170,17 +169,23 @@ public class WallpaperCropActivity extends Activity implements Handler.Callback @Override public Bitmap forPixelCount(int count) { + Bitmap bitmapToReuse = null; + // Find the smallest bitmap that satisfies the pixel count limit synchronized (mReusableBitmaps) { - Iterator itr = mReusableBitmaps.iterator(); - while (itr.hasNext()) { - Bitmap b = itr.next(); - if (b.getWidth() * b.getHeight() >= count) { - itr.remove(); - return b; + int currentBitmapSize = Integer.MAX_VALUE; + for (Bitmap b : mReusableBitmaps) { + int bitmapSize = b.getWidth() * b.getHeight(); + if ((bitmapSize >= count) && (bitmapSize < currentBitmapSize)) { + bitmapToReuse = b; + currentBitmapSize = bitmapSize; } } + + if (bitmapToReuse != null) { + mReusableBitmaps.remove(bitmapToReuse); + } } - return null; + return bitmapToReuse; } }); } catch (SecurityException securityException) { -- cgit v1.2.3