Prevent LocalImageResolver from upscaling images

LocalImageResolver should not upscale the image if it's already smaller than the maximum width.

Bug: 228719813
Test: atest LocalImageResolverTest
      New test failed before change / passed after change
Change-Id: I4fc09d1955fe94159cf0e2e8125044712f3c3134
This commit is contained in:
Jernej Virag
2022-04-21 07:22:41 +00:00
parent 82e82e1479
commit dd397eac49
2 changed files with 16 additions and 0 deletions

View File

@@ -198,6 +198,11 @@ public class LocalImageResolver {
}
final Size size = info.getSize();
if (size.getWidth() <= maxWidth && size.getHeight() <= maxHeight) {
// We don't want to upscale images needlessly.
return;
}
if (size.getWidth() > size.getHeight()) {
if (size.getWidth() > maxWidth) {
final int targetHeight = size.getHeight() * maxWidth / size.getWidth();

View File

@@ -194,6 +194,17 @@ public class LocalImageResolverTest {
assertThat(bd.getBitmap().getHeight()).isGreaterThan(51);
}
@Test
public void resolveImage_smallBitmapIcon_passedSmallerSize_dontResize() {
Icon icon = Icon.createWithResource(mContext.getResources(), R.drawable.test32x24);
Drawable d = LocalImageResolver.resolveImage(icon, mContext, 600, 450);
assertThat(d).isInstanceOf(BitmapDrawable.class);
BitmapDrawable bd = (BitmapDrawable) d;
assertThat(bd.getBitmap().getWidth()).isEqualTo(32);
assertThat(bd.getBitmap().getHeight()).isEqualTo(24);
}
@Test
public void resolveImage_largeBitmapIcon_passedSize_resizeToDefinedSize() {
Icon icon = Icon.createWithBitmap(