From 435de332b89567c88e305c055698a8146502a98a Mon Sep 17 00:00:00 2001 From: Christopher Eby Date: Thu, 1 Jul 2010 00:08:40 -0500 Subject: [PATCH] Reuse bitmaps in CoverView --- src/org/kreed/vanilla/CoverBitmap.java | 39 +++++++++++++++++++++++--- src/org/kreed/vanilla/CoverView.java | 5 ++-- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/org/kreed/vanilla/CoverBitmap.java b/src/org/kreed/vanilla/CoverBitmap.java index ca89a5a6..60460ee4 100644 --- a/src/org/kreed/vanilla/CoverBitmap.java +++ b/src/org/kreed/vanilla/CoverBitmap.java @@ -22,6 +22,7 @@ import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; +import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; @@ -192,10 +193,13 @@ public final class CoverBitmap { * @param song The song to display information for * @param width Maximum width of image * @param height Maximum height of image + * @param bitmap A Bitmap to be drawn into. If null, a new Bitmap will be + * created. If too small, will be recycled and a new Bitmap will be + * created. * @return The image, or null if the song was null, or width or height * were less than 1 */ - public static Bitmap createOverlappingBitmap(Song song, int width, int height) + public static Bitmap createOverlappingBitmap(Song song, int width, int height, Bitmap bitmap) { if (song == null || width < 1 || height < 1) return null; @@ -247,7 +251,19 @@ public final class CoverBitmap { int bitmapWidth = Math.max(coverWidth, boxWidth); int bitmapHeight = Math.max(coverHeight, boxHeight); - Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.RGB_565); + if (bitmap != null) { + if (bitmap.getHeight() < bitmapHeight || bitmap.getWidth() < bitmapWidth) { + bitmap.recycle(); + bitmap = null; + } else { + bitmap.eraseColor(Color.BLACK); + bitmapHeight = bitmap.getHeight(); + bitmapWidth = bitmap.getWidth(); + } + } + + if (bitmap == null) + bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); if (cover != null) { @@ -290,10 +306,13 @@ public final class CoverBitmap { * @param song The song to display information for * @param width Maximum width of image * @param height Maximum height of image + * @param bitmap A Bitmap to be drawn into. If null, a new Bitmap will be + * created. If too small, will be recycled and a new Bitmap will be + * created. * @return The image, or null if the song was null, or width or height * were less than 1 */ - public static Bitmap createSeparatedBitmap(Song song, int width, int height) + public static Bitmap createSeparatedBitmap(Song song, int width, int height, Bitmap bitmap) { if (song == null || width < 1 || height < 1) return null; @@ -349,7 +368,19 @@ public final class CoverBitmap { int bitmapWidth = (int)(horizontal ? coverWidth + boxWidth : Math.max(coverWidth, boxWidth)); int bitmapHeight = (int)(horizontal ? Math.max(coverHeight, boxHeight) : coverHeight + boxHeight); - Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.RGB_565); + if (bitmap != null) { + if (bitmap.getHeight() < bitmapHeight || bitmap.getWidth() < bitmapWidth) { + bitmap.recycle(); + bitmap = null; + } else { + bitmap.eraseColor(Color.BLACK); + bitmapHeight = bitmap.getHeight(); + bitmapWidth = bitmap.getWidth(); + } + } + + if (bitmap == null) + bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); if (cover != null) { diff --git a/src/org/kreed/vanilla/CoverView.java b/src/org/kreed/vanilla/CoverView.java index 6ef75625..782cd567 100644 --- a/src/org/kreed/vanilla/CoverView.java +++ b/src/org/kreed/vanilla/CoverView.java @@ -316,10 +316,11 @@ public final class CoverView extends View implements Handler.Callback { { Bitmap bitmap = mBitmapCache.get(song.id); if (bitmap == null) { + bitmap = mBitmapCache.discardOldest(); if (mSeparateInfo) - bitmap = CoverBitmap.createSeparatedBitmap(song, getWidth(), getHeight()); + bitmap = CoverBitmap.createSeparatedBitmap(song, getWidth(), getHeight(), bitmap); else - bitmap = CoverBitmap.createOverlappingBitmap(song, getWidth(), getHeight()); + bitmap = CoverBitmap.createOverlappingBitmap(song, getWidth(), getHeight(), bitmap); mBitmapCache.put(song.id, bitmap); postInvalidate(); } else {