fix(android): background image aspect ratio (#10651)

This commit is contained in:
Dimitris-Rafail Katsampas
2025-01-13 04:00:23 +02:00
committed by GitHub
parent 5e85d8873c
commit eb21056a64
3 changed files with 15 additions and 3 deletions

View File

@ -273,10 +273,11 @@ public class BorderDrawable extends ColorDrawable implements BitmapOwner {
Fetcher fetcher = Fetcher.getInstance(context);
// TODO: Implement option to pass load-mode like in ImageView class.
boolean loadAsync = backgroundImageUri.startsWith("http");
fetcher.loadImage(backgroundImageUri, this, 0, 0, false, true, loadAsync, null);
}
}
// Maintain aspect ratio for background images by default and size will be handled by border drawable
fetcher.loadImage(backgroundImageUri, this, 0, 0, true, true, loadAsync, null);
}
}
RectF backgroundBoundsF = new RectF();
Path backgroundPath = new Path();

View File

@ -546,11 +546,14 @@ public class Fetcher extends Worker {
int sourceWidth = bitmap.getWidth();
int sourceHeight = bitmap.getHeight();
reqWidth = reqWidth > 0 ? reqWidth : Math.min(sourceWidth, mDeviceWidthPixels);
reqHeight = reqHeight > 0 ? reqHeight : Math.min(sourceHeight, mDeviceHeightPixels);
// scale
if (reqWidth != sourceWidth || reqHeight != sourceHeight) {
boolean needsResize;
if (keepAspectRatio) {
double widthCoef = (double) sourceWidth / (double) reqWidth;
double heightCoef = (double) sourceHeight / (double) reqHeight;
@ -558,10 +561,18 @@ public class Fetcher extends Worker {
reqWidth = (int) Math.floor(sourceWidth / aspectCoef);
reqHeight = (int) Math.floor(sourceHeight / aspectCoef);
// Update resize flag as values might revert back to original
needsResize = reqWidth != sourceWidth || reqHeight != sourceHeight;
} else {
needsResize = true;
}
// After preserving aspect ratio, check if bitmap still needs scaling
if (needsResize) {
bitmap = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true);
}
}
// rotate
if (ei != null) {