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 696f6529..b9cdf235 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 @@ -44,14 +44,9 @@ public class AsyncDrawable extends Drawable { if (placeholder != null) { // process placeholder bounds - final Rect bounds = placeholder.getBounds(); - if (bounds.isEmpty()) { - // set intrinsic bounds - final Rect rect = new Rect( - 0, - 0, - placeholder.getIntrinsicWidth(), - placeholder.getIntrinsicHeight()); + if (placeholder.getBounds().isEmpty()) { + // set intrinsic bounds for both drawables (this one and placeholder) + final Rect rect = DrawableUtils.intrinsicBounds(placeholder); placeholder.setBounds(rect); setBounds(rect); } @@ -123,6 +118,24 @@ public class AsyncDrawable extends Drawable { initBounds(); } + /** + * Remove result from this drawable (for example, in case of cancellation) + * + * @since 3.0.1-SNAPSHOT + */ + public void clearResult() { + + final Drawable result = this.result; + + if (result != null) { + result.setCallback(null); + this.result = null; + + // clear bounds + setBounds(0, 0, 0, 0); + } + } + private void initBounds() { if (canvasWidth == 0) { diff --git a/markwon-core/src/main/java/ru/noties/markwon/image/DrawableUtils.java b/markwon-core/src/main/java/ru/noties/markwon/image/DrawableUtils.java new file mode 100644 index 00000000..9ccf11c4 --- /dev/null +++ b/markwon-core/src/main/java/ru/noties/markwon/image/DrawableUtils.java @@ -0,0 +1,25 @@ +package ru.noties.markwon.image; + +import android.graphics.Rect; +import android.graphics.drawable.Drawable; +import android.support.annotation.NonNull; + +/** + * @since 3.0.1-SNAPSHOT + */ +public abstract class DrawableUtils { + + @NonNull + public static Rect intrinsicBounds(@NonNull Drawable drawable) { + return new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); + } + + public static void applyIntrinsicBoundsIfEmpty(@NonNull Drawable drawable) { + if (drawable.getBounds().isEmpty()) { + drawable.setBounds(intrinsicBounds(drawable)); + } + } + + private DrawableUtils() { + } +}