Skip to content

Commit

Permalink
Draw bitmaps at their composition size rather than bitmap size
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Peal committed Dec 25, 2020
1 parent 937148b commit 8483fcd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
31 changes: 31 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
Expand Up @@ -1067,7 +1067,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) {
Expand All @@ -1076,6 +1080,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.
Expand Down
Expand Up @@ -66,6 +66,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) {
Expand Down
Expand Up @@ -11,6 +11,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;
Expand All @@ -23,15 +24,17 @@ 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<ColorFilter, ColorFilter> colorFilterAnimation;

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();
Expand All @@ -43,24 +46,23 @@ 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);
}
}

@Nullable
private Bitmap getBitmap() {
String refId = layerModel.getRefId();
return lottieDrawable.getImageAsset(refId);
return lottieDrawable.getBitmapForId(refId);
}

@SuppressWarnings("SingleStatementInBlock")
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/assets/Tests/ImageScaleUpScaleDown.json

Large diffs are not rendered by default.

0 comments on commit 8483fcd

Please sign in to comment.