1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-16 13:47:47 +01:00

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 */
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);