From 568559614b065af7060120306fd3c23239ffa877 Mon Sep 17 00:00:00 2001 From: ajp_anton Date: Mon, 4 May 2026 14:09:38 +0000 Subject: [PATCH] Preserve notification icon aspect ratio --- .../runtime/render/SnapshotRenderer.java | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/se/ajpanton/statusbartweak/runtime/render/SnapshotRenderer.java b/app/src/main/java/se/ajpanton/statusbartweak/runtime/render/SnapshotRenderer.java index c6b2531..6560cbf 100644 --- a/app/src/main/java/se/ajpanton/statusbartweak/runtime/render/SnapshotRenderer.java +++ b/app/src/main/java/se/ajpanton/statusbartweak/runtime/render/SnapshotRenderer.java @@ -588,18 +588,42 @@ final class SnapshotRenderer { return false; } Rect oldBounds = new Rect(drawable.getBounds()); - int size = Math.min(width, height); - if (size <= 0) { - size = Math.max(width, height); - } - int left = Math.max(0, (width - size) / 2); - int top = Math.max(0, (height - size) / 2); - drawable.setBounds(left, top, left + size, top + size); + Rect drawBounds = aspectFitBounds(drawable, oldBounds, width, height); + drawable.setBounds(drawBounds); drawable.draw(canvas); drawable.setBounds(oldBounds); return true; } + private Rect aspectFitBounds(Drawable drawable, Rect fallbackBounds, int width, int height) { + int sourceWidth = drawable.getIntrinsicWidth(); + int sourceHeight = drawable.getIntrinsicHeight(); + if (sourceWidth <= 0 || sourceHeight <= 0) { + sourceWidth = fallbackBounds != null ? fallbackBounds.width() : 0; + sourceHeight = fallbackBounds != null ? fallbackBounds.height() : 0; + } + if (sourceWidth <= 0 || sourceHeight <= 0) { + int size = Math.max(1, Math.min(width, height)); + int left = Math.max(0, (width - size) / 2); + int top = Math.max(0, (height - size) / 2); + return new Rect(left, top, left + size, top + size); + } + float sourceAspect = (float) sourceWidth / sourceHeight; + float targetAspect = (float) width / height; + int drawWidth; + int drawHeight; + if (sourceAspect > targetAspect) { + drawWidth = width; + drawHeight = Math.max(1, Math.round(width / sourceAspect)); + } else { + drawHeight = height; + drawWidth = Math.max(1, Math.round(height * sourceAspect)); + } + int left = Math.max(0, (width - drawWidth) / 2); + int top = Math.max(0, (height - drawHeight) / 2); + return new Rect(left, top, left + drawWidth, top + drawHeight); + } + private Drawable findDrawable(View source) { if (source instanceof ImageView imageView && imageView.getDrawable() != null) { return imageView.getDrawable();