Reuse bitmaps in CoverView

This commit is contained in:
Christopher Eby 2010-07-01 00:08:40 -05:00
parent a85848c246
commit 435de332b8
2 changed files with 38 additions and 6 deletions

View File

@ -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) {

View File

@ -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 {