From a6b49971953cb87b9b35b1f4b1069c02900adb16 Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Tue, 2 Feb 2016 14:03:49 +0100 Subject: [PATCH] Move placeholder cover code to CoverBitmap --- .../android/vanilla/CoverBitmap.java | 41 ++++++++++++++++ .../android/vanilla/LazyCoverView.java | 49 +------------------ 2 files changed, 42 insertions(+), 48 deletions(-) diff --git a/src/ch/blinkenlights/android/vanilla/CoverBitmap.java b/src/ch/blinkenlights/android/vanilla/CoverBitmap.java index 420807c3..b8e4c6f0 100644 --- a/src/ch/blinkenlights/android/vanilla/CoverBitmap.java +++ b/src/ch/blinkenlights/android/vanilla/CoverBitmap.java @@ -24,6 +24,7 @@ package ch.blinkenlights.android.vanilla; import android.content.Context; import android.content.res.Resources; +import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; @@ -384,4 +385,44 @@ public final class CoverBitmap { return bitmap; } + + /** + * Draws a placeholder cover from given title string + * + * @param title A text string to use in the cover + * @return bitmap The drawn bitmap + */ + public static Bitmap generatePlaceholderCover(Context context, int width, int height, String title) + { + if (title == null || width < 1 || height < 1) + return null; + + final float textSize = width * 0.4f; + + title = title.replaceFirst("(?i)^The ", ""); // 'The\s' shall not be a part of the string we are drawing. + String subText = (title+" ").substring(0,2); + + Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); + Canvas canvas = new Canvas(bitmap); + Paint paint = new Paint(); + + // Picks a semi-random color from tiles_colors.xml + TypedArray colors = context.getResources().obtainTypedArray(R.array.letter_tile_colors); + int color = colors.getColor(Math.abs(title.hashCode()) % colors.length(), 0); + colors.recycle(); + paint.setColor(color); + + paint.setStyle(Paint.Style.FILL); + canvas.drawPaint(paint); + + paint.setARGB(255, 255, 255, 255); + paint.setAntiAlias(true); + paint.setTextSize(textSize); + + Rect bounds = new Rect(); + paint.getTextBounds(subText, 0, subText.length(), bounds); + + canvas.drawText(subText, (width/2f)-bounds.exactCenterX(), (height/2f)-bounds.exactCenterY(), paint); + return bitmap; + } } diff --git a/src/ch/blinkenlights/android/vanilla/LazyCoverView.java b/src/ch/blinkenlights/android/vanilla/LazyCoverView.java index 28e56c85..9f5bb026 100644 --- a/src/ch/blinkenlights/android/vanilla/LazyCoverView.java +++ b/src/ch/blinkenlights/android/vanilla/LazyCoverView.java @@ -18,18 +18,13 @@ package ch.blinkenlights.android.vanilla; import android.content.Context; -import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.graphics.drawable.TransitionDrawable; import android.graphics.drawable.BitmapDrawable; -import android.graphics.Paint; -import android.graphics.Rect; import android.os.Handler; import android.os.HandlerThread; -import android.os.Looper; import android.os.Message; import android.util.AttributeSet; import android.util.LruCache; @@ -151,7 +146,7 @@ public class LazyCoverView extends ImageView bitmap = song.getSmallCover(mContext); } } else { - bitmap = drawCoverFromString(payload.title); + bitmap = CoverBitmap.generatePlaceholderCover(mContext, CoverCache.SIZE_SMALL, CoverCache.SIZE_SMALL, payload.title); } if (bitmap == null) { // item has no cover: return a failback @@ -216,48 +211,6 @@ public class LazyCoverView extends ImageView return cacheHit; } - /** - * Draws a placeholder cover from given title string - * - * @param title A text string to use in the cover - * @return bitmap The drawn bitmap - */ - private Bitmap drawCoverFromString(String title) { - if (title == null) - return null; - - final int canvasSize = CoverCache.SIZE_SMALL; - final float textSize = canvasSize * 0.45f; - - title = title.replaceFirst("(?i)^The ", ""); // 'The\s' shall not be a part of the string we are drawing. - String subText = (title+" ").substring(0,2); - - Bitmap bitmap = Bitmap.createBitmap(canvasSize, canvasSize, Bitmap.Config.RGB_565); - Canvas canvas = new Canvas(bitmap); - Paint paint = new Paint(); - - // Picks a semi-random color from tiles_colors.xml - TypedArray colors = getResources().obtainTypedArray(R.array.letter_tile_colors); - int color = colors.getColor(Math.abs(title.hashCode()) % colors.length(), 0); - colors.recycle(); - paint.setColor(color); - - paint.setStyle(Paint.Style.FILL); - canvas.drawPaint(paint); - - paint.setARGB(255, 255, 255, 255); - paint.setAntiAlias(true); - paint.setTextSize(textSize); - - Rect bounds = new Rect(); - paint.getTextBounds(subText, 0, subText.length(), bounds); - - canvas.drawText(subText, (canvasSize/2f)-bounds.exactCenterX(), (canvasSize/2f)-bounds.exactCenterY(), paint); - return bitmap; - - } - - /** * A LRU cache implementation, using the CoverKey as key to store Bitmap objects *