Bangle.js restrict icon size even if it's already a bitmap

This commit is contained in:
Gordon Williams 2022-02-10 14:19:32 +00:00
parent 9e8b7373fa
commit eddbde313b

View File

@ -624,23 +624,34 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
/** Convert a drawable to a bitmap, for use with bitmapToEspruino */ /** Convert a drawable to a bitmap, for use with bitmapToEspruino */
public static Bitmap drawableToBitmap(Drawable drawable) { public static Bitmap drawableToBitmap(Drawable drawable) {
final int maxWidth = 32;
final int maxHeight = 32;
/* Return bitmap directly but only if it's small enough. It could be
we have a bitmap but it's just too big to send direct to the bangle */
if (drawable instanceof BitmapDrawable) { if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if (bitmapDrawable.getBitmap() != null) { Bitmap bmp = bitmapDrawable.getBitmap();
return bitmapDrawable.getBitmap(); if (bmp != null && bmp.getWidth()<=maxWidth && bmp.getHeight()<=maxHeight)
} return bmp;
} }
int w = 24; /* Otherwise render this to a bitmap ourselves.. work out size */
int h = 24; int w = maxWidth;
int h = maxHeight;
if (drawable.getIntrinsicWidth() > 0 && drawable.getIntrinsicHeight() > 0) { if (drawable.getIntrinsicWidth() > 0 && drawable.getIntrinsicHeight() > 0) {
w = drawable.getIntrinsicWidth(); w = drawable.getIntrinsicWidth();
h = drawable.getIntrinsicHeight(); h = drawable.getIntrinsicHeight();
// don't allocate anything too big // don't allocate anything too big, but keep the ratio
if (w>24) w=24; if (w>maxWidth) {
if (h>24) h=24; h = h * maxWidth / w;
w = maxWidth;
}
if (h>maxHeight) {
w = w * maxHeight / h;
h = maxHeight;
}
} }
/* render */
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
Canvas canvas = new Canvas(bitmap); Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas); drawable.draw(canvas);