diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java b/lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java index 5882ef2013..5facb2f33c 100644 --- a/lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java +++ b/lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java @@ -1045,7 +1045,11 @@ public Bitmap updateBitmap(String id, @Nullable Bitmap bitmap) { return ret; } + /** + * @deprecated use {@link #getBitmapForId(String)}. + */ @Nullable + @Deprecated public Bitmap getImageAsset(String id) { ImageAssetManager bm = getImageAssetManager(); if (bm != null) { @@ -1058,6 +1062,33 @@ public Bitmap getImageAsset(String id) { return null; } + /** + * Returns the bitmap that will be rendered for the given id in the Lottie animation file. + * The returned bitmap could be from: + * * Embedded in the animation file as a base64 string. + * * In the same directory as the animation file. + * * In the same zip file as the animation file. + * * Returned from an {@link ImageAssetDelegate}. + * or null if the image doesn't exist from any of those places. + */ + @Nullable + public Bitmap getBitmapForId(String id) { + ImageAssetManager assetManager = getImageAssetManager(); + if (assetManager != null) { + return assetManager.bitmapForId(id); + } + return null; + } + + @Nullable + public LottieImageAsset getLottieImageAssetForId(String id) { + ImageAssetManager assetManager = getImageAssetManager(); + if (assetManager != null) { + return assetManager.getImageAssetById(id); + } + return null; + } + private ImageAssetManager getImageAssetManager() { if (getCallback() == null) { // We can't get a bitmap since we can't get a Context from the callback. diff --git a/lottie/src/main/java/com/airbnb/lottie/manager/ImageAssetManager.java b/lottie/src/main/java/com/airbnb/lottie/manager/ImageAssetManager.java index d28061d8c0..0634357045 100644 --- a/lottie/src/main/java/com/airbnb/lottie/manager/ImageAssetManager.java +++ b/lottie/src/main/java/com/airbnb/lottie/manager/ImageAssetManager.java @@ -62,6 +62,10 @@ public void setDelegate(@Nullable ImageAssetDelegate assetDelegate) { return prevBitmap; } + @Nullable public LottieImageAsset getImageAssetById(String id) { + return imageAssets.get(id); + } + @Nullable public Bitmap bitmapForId(String id) { LottieImageAsset asset = imageAssets.get(id); if (asset == null) { diff --git a/lottie/src/main/java/com/airbnb/lottie/model/layer/ImageLayer.java b/lottie/src/main/java/com/airbnb/lottie/model/layer/ImageLayer.java index 16534495f3..068b109392 100644 --- a/lottie/src/main/java/com/airbnb/lottie/model/layer/ImageLayer.java +++ b/lottie/src/main/java/com/airbnb/lottie/model/layer/ImageLayer.java @@ -12,6 +12,7 @@ import androidx.annotation.Nullable; import com.airbnb.lottie.LottieDrawable; +import com.airbnb.lottie.LottieImageAsset; import com.airbnb.lottie.LottieProperty; import com.airbnb.lottie.animation.LPaint; import com.airbnb.lottie.animation.keyframe.BaseKeyframeAnimation; @@ -24,16 +25,18 @@ public class ImageLayer extends BaseLayer { private final Paint paint = new LPaint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); private final Rect src = new Rect(); private final Rect dst = new Rect(); + @Nullable private final LottieImageAsset lottieImageAsset; @Nullable private BaseKeyframeAnimation colorFilterAnimation; @Nullable private BaseKeyframeAnimation imageAnimation; ImageLayer(LottieDrawable lottieDrawable, Layer layerModel) { super(lottieDrawable, layerModel); + lottieImageAsset = lottieDrawable.getLottieImageAssetForId(layerModel.getRefId()); } @Override public void drawLayer(@NonNull Canvas canvas, Matrix parentMatrix, int parentAlpha) { Bitmap bitmap = getBitmap(); - if (bitmap == null || bitmap.isRecycled()) { + if (bitmap == null || bitmap.isRecycled() || lottieImageAsset == null) { return; } float density = Utils.dpScale(); @@ -45,16 +48,15 @@ public class ImageLayer extends BaseLayer { canvas.save(); canvas.concat(parentMatrix); src.set(0, 0, bitmap.getWidth(), bitmap.getHeight()); - dst.set(0, 0, (int) (bitmap.getWidth() * density), (int) (bitmap.getHeight() * density)); + dst.set(0, 0, (int) (lottieImageAsset.getWidth() * density), (int) (lottieImageAsset.getHeight() * density)); canvas.drawBitmap(bitmap, src, dst, paint); canvas.restore(); } @Override public void getBounds(RectF outBounds, Matrix parentMatrix, boolean applyParents) { super.getBounds(outBounds, parentMatrix, applyParents); - Bitmap bitmap = getBitmap(); - if (bitmap != null) { - outBounds.set(0, 0, bitmap.getWidth() * Utils.dpScale(), bitmap.getHeight() * Utils.dpScale()); + if (lottieImageAsset != null) { + outBounds.set(0, 0, lottieImageAsset.getWidth() * Utils.dpScale(), lottieImageAsset.getHeight() * Utils.dpScale()); boundsMatrix.mapRect(outBounds); } } @@ -67,7 +69,7 @@ private Bitmap getBitmap() { return callbackBitmap; } String refId = layerModel.getRefId(); - return lottieDrawable.getImageAsset(refId); + return lottieDrawable.getBitmapForId(refId); } @SuppressWarnings("SingleStatementInBlock")