Render default cover programatically

This commit is contained in:
Christopher Eby 2011-10-25 04:07:17 -05:00
parent cba7b0125d
commit 895740ea36
5 changed files with 51 additions and 27 deletions

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.0 KiB

View File

@ -28,8 +28,11 @@ import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.DisplayMetrics;
import android.util.TypedValue;
@ -410,4 +413,47 @@ public final class CoverBitmap {
source.recycle();
return result;
}
/**
* Generate the default cover (a rendition of a CD). Returns a square iamge.
* Both dimensions are the lesser of width and height.
*
* @param width The max width
* @param height The max height
* @return The default cover.
*/
public static Bitmap generateDefaultCover(int width, int height)
{
int size = Math.min(width, height);
int halfSize = size / 2;
int eightSize = size / 8;
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565);
LinearGradient gradient = new LinearGradient(size, 0, 0, size, 0xff646464, 0xff464646, Shader.TileMode.CLAMP);
RectF oval = new RectF(eightSize, 0, size - eightSize, size);
Paint paint = new Paint();
paint.setAntiAlias(true);
Canvas canvas = new Canvas(bitmap);
canvas.rotate(-45, halfSize, halfSize);
paint.setShader(gradient);
canvas.translate(size / 20, size / 20);
canvas.scale(0.9f, 0.9f);
canvas.drawOval(oval, paint);
paint.setShader(null);
paint.setColor(0xff000000);
canvas.translate(size / 3, size / 3);
canvas.scale(0.333f, 0.333f);
canvas.drawOval(oval, paint);
paint.setShader(gradient);
canvas.translate(size / 3, size / 3);
canvas.scale(0.333f, 0.333f);
canvas.drawOval(oval, paint);
return bitmap;
}
}

View File

@ -24,7 +24,6 @@ package org.kreed.vanilla;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
@ -137,29 +136,6 @@ public final class CoverView extends View implements Handler.Callback {
scrollTo(getWidth(), 0);
}
/**
* Create the cover to show when a song has no cover art. Only used in the
* no info modes.
*
* @return The default cover, now also stored in mDefaultCover.
*/
private Bitmap generateDefaultCover()
{
int style = mCoverStyle;
Bitmap result = null;
if (style == CoverBitmap.STYLE_NO_INFO || style == CoverBitmap.STYLE_NO_INFO_ZOOMED) {
Context context = getContext();
Bitmap def = BitmapFactory.decodeResource(context.getResources(), R.drawable.default_cover);
int width = getWidth();
int height = getHeight();
result = CoverBitmap.createBitmap(context, style, def, null, width, height, null);
}
mDefaultCover = result;
return result;
}
@Override
protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight)
{
@ -344,14 +320,16 @@ public final class CoverView extends View implements Handler.Callback {
int style = mCoverStyle;
Context context = getContext();
Bitmap cover = song.getCover(context);
int width = getWidth();
int height = getHeight();
Bitmap bitmap;
if (cover == null && (style == CoverBitmap.STYLE_NO_INFO || style == CoverBitmap.STYLE_NO_INFO_ZOOMED)) {
if (mDefaultCover == null)
mDefaultCover = CoverBitmap.generateDefaultCover(width, height);
bitmap = mDefaultCover;
if (bitmap == null)
bitmap = generateDefaultCover();
} else {
bitmap = CoverBitmap.createBitmap(context, style, cover, song, getWidth(), getHeight(), reuse);
bitmap = CoverBitmap.createBitmap(context, style, cover, song, width, height, reuse);
}
mBitmaps[i] = bitmap;