From eddbde313b041e66f4b2a9c593ea78eddb607238 Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Thu, 10 Feb 2022 14:19:32 +0000 Subject: [PATCH] Bangle.js restrict icon size even if it's already a bitmap --- .../banglejs/BangleJSDeviceSupport.java | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java index 51357d254..eb0754e23 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java @@ -624,23 +624,34 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport { /** Convert a drawable to a bitmap, for use with bitmapToEspruino */ 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) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; - if (bitmapDrawable.getBitmap() != null) { - return bitmapDrawable.getBitmap(); - } + Bitmap bmp = bitmapDrawable.getBitmap(); + if (bmp != null && bmp.getWidth()<=maxWidth && bmp.getHeight()<=maxHeight) + return bmp; } - int w = 24; - int h = 24; + /* Otherwise render this to a bitmap ourselves.. work out size */ + int w = maxWidth; + int h = maxHeight; if (drawable.getIntrinsicWidth() > 0 && drawable.getIntrinsicHeight() > 0) { w = drawable.getIntrinsicWidth(); h = drawable.getIntrinsicHeight(); - // don't allocate anything too big - if (w>24) w=24; - if (h>24) h=24; + // don't allocate anything too big, but keep the ratio + if (w>maxWidth) { + 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 - Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas);