Skip to content

fix(android): Background image incorrect aspect ratio #10651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified packages/core/platforms/android/widgets-release.aar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,12 @@ public void refresh(int borderTopColor,
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();
RectF backgroundRect = new RectF();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,21 +546,32 @@ private static Bitmap scaleAndRotateBitmap(Bitmap bitmap, ExifInterface ei, int

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;
double aspectCoef = Math.min(widthCoef, heightCoef);

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;
}

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
Expand Down
Loading