Move placeholder cover code to CoverBitmap
This commit is contained in:
parent
f49d5fd1ad
commit
a6b4997195
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user