Fit an image without dimensions to canvas width (and keep ratio)

This commit is contained in:
Dimitry Ivanov 2018-04-15 19:53:30 +03:00
parent af7d58c59a
commit 31506a7acd

View File

@ -52,6 +52,7 @@ public class AsyncDrawable extends Drawable {
this.imageSize = imageSize; this.imageSize = imageSize;
} }
@NonNull
public String getDestination() { public String getDestination() {
return destination; return destination;
} }
@ -172,13 +173,29 @@ public class AsyncDrawable extends Drawable {
*/ */
@NonNull @NonNull
private Rect resolveBounds() { private Rect resolveBounds() {
final Rect rect; final Rect rect;
if (imageSizeResolver == null if (imageSizeResolver == null
|| imageSize == null) { || imageSize == null) {
rect = result.getBounds();
// @since 1.0.5
final Rect bounds = result.getBounds();
if (bounds.width() > canvasWidth) {
// let's scale image down, as we do not want to expand beyond canvas width
final float ratio = (float) bounds.width() / bounds.height();
final int height = (int) (canvasWidth / ratio + .5F);
rect = new Rect(0, 0, canvasWidth, height);
} else {
rect = bounds;
}
} else { } else {
rect = imageSizeResolver.resolveImageSize(imageSize, result.getBounds(), canvasWidth, textSize); rect = imageSizeResolver.resolveImageSize(imageSize, result.getBounds(), canvasWidth, textSize);
} }
return rect; return rect;
} }
} }