Preserve notification icon aspect ratio
This commit is contained in:
@@ -588,18 +588,42 @@ final class SnapshotRenderer {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Rect oldBounds = new Rect(drawable.getBounds());
|
Rect oldBounds = new Rect(drawable.getBounds());
|
||||||
int size = Math.min(width, height);
|
Rect drawBounds = aspectFitBounds(drawable, oldBounds, width, height);
|
||||||
if (size <= 0) {
|
drawable.setBounds(drawBounds);
|
||||||
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);
|
|
||||||
drawable.draw(canvas);
|
drawable.draw(canvas);
|
||||||
drawable.setBounds(oldBounds);
|
drawable.setBounds(oldBounds);
|
||||||
return true;
|
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) {
|
private Drawable findDrawable(View source) {
|
||||||
if (source instanceof ImageView imageView && imageView.getDrawable() != null) {
|
if (source instanceof ImageView imageView && imageView.getDrawable() != null) {
|
||||||
return imageView.getDrawable();
|
return imageView.getDrawable();
|
||||||
|
|||||||
Reference in New Issue
Block a user