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,11 +273,12 @@ public class BorderDrawable extends ColorDrawable implements BitmapOwner {
Fetcher fetcher = Fetcher.getInstance(context); Fetcher fetcher = Fetcher.getInstance(context);
// TODO: Implement option to pass load-mode like in ImageView class. // TODO: Implement option to pass load-mode like in ImageView class.
boolean loadAsync = backgroundImageUri.startsWith("http"); 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(); RectF backgroundBoundsF = new RectF();
Path backgroundPath = new Path(); Path backgroundPath = new Path();
RectF backgroundRect = new RectF(); RectF backgroundRect = new RectF();

View File

@ -546,11 +546,14 @@ public class Fetcher extends Worker {
int sourceWidth = bitmap.getWidth(); int sourceWidth = bitmap.getWidth();
int sourceHeight = bitmap.getHeight(); int sourceHeight = bitmap.getHeight();
reqWidth = reqWidth > 0 ? reqWidth : Math.min(sourceWidth, mDeviceWidthPixels); reqWidth = reqWidth > 0 ? reqWidth : Math.min(sourceWidth, mDeviceWidthPixels);
reqHeight = reqHeight > 0 ? reqHeight : Math.min(sourceHeight, mDeviceHeightPixels); reqHeight = reqHeight > 0 ? reqHeight : Math.min(sourceHeight, mDeviceHeightPixels);
// scale // scale
if (reqWidth != sourceWidth || reqHeight != sourceHeight) { if (reqWidth != sourceWidth || reqHeight != sourceHeight) {
boolean needsResize;
if (keepAspectRatio) { if (keepAspectRatio) {
double widthCoef = (double) sourceWidth / (double) reqWidth; double widthCoef = (double) sourceWidth / (double) reqWidth;
double heightCoef = (double) sourceHeight / (double) reqHeight; double heightCoef = (double) sourceHeight / (double) reqHeight;
@ -558,9 +561,17 @@ public class Fetcher extends Worker {
reqWidth = (int) Math.floor(sourceWidth / aspectCoef); reqWidth = (int) Math.floor(sourceWidth / aspectCoef);
reqHeight = (int) Math.floor(sourceHeight / 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;
} }
bitmap = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true); // After preserving aspect ratio, check if bitmap still needs scaling
if (needsResize) {
bitmap = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true);
}
} }
// rotate // rotate