Fix DrawableUtils (intrinsic bounds)

This commit is contained in:
Dimitry Ivanov 2019-04-16 11:58:00 +03:00
parent f0a03b7df6
commit 1f3c50da03
6 changed files with 19 additions and 13 deletions

View File

@ -43,11 +43,8 @@ public class AsyncDrawable extends Drawable {
final Drawable placeholder = loader.placeholder(); final Drawable placeholder = loader.placeholder();
if (placeholder != null) { if (placeholder != null) {
// process placeholder bounds // process placeholder bounds (if it's not empty -> ignore, use whatever bounds that were set)
if (placeholder.getBounds().isEmpty()) { DrawableUtils.applyIntrinsicBoundsIfEmpty(placeholder);
final Rect rect = DrawableUtils.intrinsicBounds(placeholder);
placeholder.setBounds(rect);
}
// apply placeholder immediately if we have one // apply placeholder immediately if we have one
setResult(placeholder); setResult(placeholder);

View File

@ -2,6 +2,7 @@ package ru.noties.markwon.image;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
/** /**
@ -10,10 +11,15 @@ import android.support.annotation.NonNull;
public abstract class DrawableUtils { public abstract class DrawableUtils {
@NonNull @NonNull
@CheckResult
public static Rect intrinsicBounds(@NonNull Drawable drawable) { public static Rect intrinsicBounds(@NonNull Drawable drawable) {
return new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); return new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
} }
public static void applyIntrinsicBounds(@NonNull Drawable drawable) {
drawable.setBounds(intrinsicBounds(drawable));
}
public static void applyIntrinsicBoundsIfEmpty(@NonNull Drawable drawable) { public static void applyIntrinsicBoundsIfEmpty(@NonNull Drawable drawable) {
if (drawable.getBounds().isEmpty()) { if (drawable.getBounds().isEmpty()) {
drawable.setBounds(intrinsicBounds(drawable)); drawable.setBounds(intrinsicBounds(drawable));

View File

@ -10,8 +10,6 @@ import android.support.annotation.Nullable;
import java.io.InputStream; import java.io.InputStream;
import ru.noties.markwon.utils.DrawableUtils;
/** /**
* This class can be used as the last {@link MediaDecoder} to _try_ to handle all rest cases. * This class can be used as the last {@link MediaDecoder} to _try_ to handle all rest cases.
* Here we just assume that supplied InputStream is of image type and try to decode it. * Here we just assume that supplied InputStream is of image type and try to decode it.
@ -42,7 +40,7 @@ public class ImageMediaDecoder extends MediaDecoder {
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
if (bitmap != null) { if (bitmap != null) {
out = new BitmapDrawable(resources, bitmap); out = new BitmapDrawable(resources, bitmap);
DrawableUtils.intrinsicBounds(out); DrawableUtils.applyIntrinsicBounds(out);
} else { } else {
out = null; out = null;
} }

View File

@ -3,11 +3,16 @@ package ru.noties.markwon.utils;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
/**
* @deprecated Please use {@link ru.noties.markwon.image.DrawableUtils}
*/
@Deprecated
public abstract class DrawableUtils { public abstract class DrawableUtils {
public static void intrinsicBounds(@NonNull Drawable drawable) { public static void intrinsicBounds(@NonNull Drawable drawable) {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
} }
private DrawableUtils() {} private DrawableUtils() {
}
} }

View File

@ -9,8 +9,8 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import pl.droidsonroids.gif.GifDrawable; import pl.droidsonroids.gif.GifDrawable;
import ru.noties.markwon.image.DrawableUtils;
import ru.noties.markwon.image.MediaDecoder; import ru.noties.markwon.image.MediaDecoder;
import ru.noties.markwon.utils.DrawableUtils;
/** /**
* @since 1.1.0 * @since 1.1.0
@ -41,7 +41,7 @@ public class GifMediaDecoder extends MediaDecoder {
if (bytes != null) { if (bytes != null) {
try { try {
out = newGifDrawable(bytes); out = newGifDrawable(bytes);
DrawableUtils.intrinsicBounds(out); DrawableUtils.applyIntrinsicBounds(out);
if (!autoPlayGif) { if (!autoPlayGif) {
((GifDrawable) out).pause(); ((GifDrawable) out).pause();

View File

@ -13,8 +13,8 @@ import com.caverock.androidsvg.SVGParseException;
import java.io.InputStream; import java.io.InputStream;
import ru.noties.markwon.image.DrawableUtils;
import ru.noties.markwon.image.MediaDecoder; import ru.noties.markwon.image.MediaDecoder;
import ru.noties.markwon.utils.DrawableUtils;
/** /**
* @since 1.1.0 * @since 1.1.0
@ -65,7 +65,7 @@ public class SvgMediaDecoder extends MediaDecoder {
svg.renderToCanvas(canvas); svg.renderToCanvas(canvas);
out = new BitmapDrawable(resources, bitmap); out = new BitmapDrawable(resources, bitmap);
DrawableUtils.intrinsicBounds(out); DrawableUtils.applyIntrinsicBounds(out);
} }
return out; return out;