From 740ff1013ca49eccdfc76c853e4c82198738efb1 Mon Sep 17 00:00:00 2001 From: Dimitry Ivanov Date: Tue, 16 Apr 2019 12:23:03 +0300 Subject: [PATCH] AsyncDrawable allow placeholder to have independent size --- .../noties/markwon/image/AsyncDrawable.java | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/markwon-core/src/main/java/ru/noties/markwon/image/AsyncDrawable.java b/markwon-core/src/main/java/ru/noties/markwon/image/AsyncDrawable.java index 2ab11fa2..2b9f5aef 100644 --- a/markwon-core/src/main/java/ru/noties/markwon/image/AsyncDrawable.java +++ b/markwon-core/src/main/java/ru/noties/markwon/image/AsyncDrawable.java @@ -42,12 +42,7 @@ public class AsyncDrawable extends Drawable { final Drawable placeholder = loader.placeholder(); if (placeholder != null) { - - // process placeholder bounds (if it's not empty -> ignore, use whatever bounds that were set) - DrawableUtils.applyIntrinsicBoundsIfEmpty(placeholder); - - // apply placeholder immediately if we have one - setResult(placeholder); + setPlaceholderResult(placeholder); } } @@ -100,6 +95,38 @@ public class AsyncDrawable extends Drawable { } } + /** + * @since 3.0.1-SNAPSHOT + */ + protected void setPlaceholderResult(@NonNull Drawable placeholder) { + // okay, if placeholder has bounds -> use it, otherwise use original imageSize + + final Rect rect = placeholder.getBounds(); + + if (rect.isEmpty()) { + // if bounds are empty -> just use placeholder as a regular result + DrawableUtils.applyIntrinsicBounds(placeholder); + setResult(placeholder); + } else { + + // this condition should not be true for placeholder (at least for now) + if (result != null) { + // but it is, unregister current result + result.setCallback(null); + } + + // placeholder has bounds specified -> use them until we have real result + this.result = placeholder; + this.result.setCallback(callback); + + // use bounds directly + setBounds(rect); + + // just in case -> so we do not update placeholder when we have canvas dimensions + waitingForDimensions = false; + } + } + public void setResult(@NonNull Drawable result) { // if we have previous one, detach it