Add DrawableUtils

This commit is contained in:
Dimitry Ivanov 2019-03-26 13:52:11 +03:00
parent 29ebfebfd8
commit a7163b8cf8
2 changed files with 46 additions and 8 deletions

View File

@ -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) {

View File

@ -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() {
}
}