From f131834fbba946d6bc6d8c1a4ca0c3a5572d38f9 Mon Sep 17 00:00:00 2001 From: Gabriel Peal Date: Fri, 29 Dec 2023 12:34:23 -0800 Subject: [PATCH 1/2] Scale image if the dp scale changes --- .../com/airbnb/lottie/LottieComposition.java | 19 ++++++++++++++++++- .../com/airbnb/lottie/LottieImageAsset.java | 13 +++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieComposition.java b/lottie/src/main/java/com/airbnb/lottie/LottieComposition.java index a5a2a8ff53..e43e2042d8 100644 --- a/lottie/src/main/java/com/airbnb/lottie/LottieComposition.java +++ b/lottie/src/main/java/com/airbnb/lottie/LottieComposition.java @@ -19,15 +19,18 @@ import com.airbnb.lottie.parser.moshi.JsonReader; import com.airbnb.lottie.utils.Logger; import com.airbnb.lottie.utils.MiscUtils; +import com.airbnb.lottie.utils.Utils; import org.json.JSONObject; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; /** * After Effects/Bodymovin composition model. This is the serialized model from which the @@ -44,6 +47,7 @@ public class LottieComposition { private final HashSet warnings = new HashSet<>(); private Map> precomps; private Map images; + private float imagesDpScale = Utils.dpScale(); /** * Map of font names to fonts */ @@ -208,8 +212,19 @@ public boolean hasImages() { * Returns a map of image asset id to {@link LottieImageAsset}. These assets contain image metadata exported * from After Effects or other design tool. The resulting Bitmaps can be set directly on the image asset so * they can be loaded once and reused across compositions. + * + * If the context dp scale has changed since the last time images were retrieved, images will be rescaled. */ public Map getImages() { + float dpScale = Utils.dpScale(); + if (dpScale != imagesDpScale) { + imagesDpScale = dpScale; + Set> entries = images.entrySet(); + + for (Map.Entry entry : entries) { + images.put(entry.getKey(), entry.getValue().copyWithScale(imagesDpScale / dpScale)); + } + } return images; } @@ -237,6 +252,7 @@ public String toString() { */ @Deprecated public static class Factory { + private Factory() { } @@ -363,6 +379,7 @@ public static LottieComposition fromJsonSync(JsonReader reader) { @SuppressWarnings("deprecation") private static final class ListenerAdapter implements LottieListener, Cancellable { + private final OnCompositionLoadedListener listener; private boolean cancelled = false; @@ -382,4 +399,4 @@ private ListenerAdapter(OnCompositionLoadedListener listener) { } } } -} \ No newline at end of file +} diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieImageAsset.java b/lottie/src/main/java/com/airbnb/lottie/LottieImageAsset.java index 39606060a5..0184f416c4 100644 --- a/lottie/src/main/java/com/airbnb/lottie/LottieImageAsset.java +++ b/lottie/src/main/java/com/airbnb/lottie/LottieImageAsset.java @@ -70,6 +70,19 @@ public void setBitmap(@Nullable Bitmap bitmap) { this.bitmap = bitmap; } + /** + * Returns a new {@link LottieImageAsset} with the same properties as this one but with the + * dimensions and bitmap scaled. + */ + public LottieImageAsset copyWithScale(float scale) { + LottieImageAsset newAsset = new LottieImageAsset((int) (width * scale), (int) (height * scale), id, fileName, dirName); + if (bitmap != null) { + Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, newAsset.width, newAsset.height, true); + newAsset.setBitmap(scaledBitmap); + } + return newAsset; + } + /** * Returns whether this asset has an embedded Bitmap or whether the fileName is a base64 encoded bitmap. */ From de11798327a3fcf1d1a4f49c732897d129507eea Mon Sep 17 00:00:00 2001 From: Gabriel Peal Date: Fri, 29 Dec 2023 14:22:51 -0800 Subject: [PATCH 2/2] Fixed test --- .../java/com/airbnb/lottie/compose/LottieClipSpecTest.kt | 3 ++- .../main/java/com/airbnb/lottie/LottieComposition.java | 5 +++-- .../lottie/parser/LottieCompositionMoshiParser.java | 2 +- .../test/java/com/airbnb/lottie/LottieDrawableTest.java | 8 ++++---- .../com/airbnb/lottie/LottieValueAnimatorUnitTest.java | 8 ++++---- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lottie-compose/src/test/java/com/airbnb/lottie/compose/LottieClipSpecTest.kt b/lottie-compose/src/test/java/com/airbnb/lottie/compose/LottieClipSpecTest.kt index 8457f54b9c..346c95d73f 100644 --- a/lottie-compose/src/test/java/com/airbnb/lottie/compose/LottieClipSpecTest.kt +++ b/lottie-compose/src/test/java/com/airbnb/lottie/compose/LottieClipSpecTest.kt @@ -135,10 +135,11 @@ class LottieClipSpecTest { LongSparseArray(), emptyMap(), emptyMap(), + 1f, SparseArrayCompat(), emptyMap(), markers, ) return composition } -} \ No newline at end of file +} diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieComposition.java b/lottie/src/main/java/com/airbnb/lottie/LottieComposition.java index e43e2042d8..7144543b2d 100644 --- a/lottie/src/main/java/com/airbnb/lottie/LottieComposition.java +++ b/lottie/src/main/java/com/airbnb/lottie/LottieComposition.java @@ -47,7 +47,7 @@ public class LottieComposition { private final HashSet warnings = new HashSet<>(); private Map> precomps; private Map images; - private float imagesDpScale = Utils.dpScale(); + private float imagesDpScale; /** * Map of font names to fonts */ @@ -75,7 +75,7 @@ public class LottieComposition { @RestrictTo(RestrictTo.Scope.LIBRARY) public void init(Rect bounds, float startFrame, float endFrame, float frameRate, List layers, LongSparseArray layerMap, Map> precomps, Map images, + List> precomps, Map images, float imagesDpScale, SparseArrayCompat characters, Map fonts, List markers) { this.bounds = bounds; @@ -86,6 +86,7 @@ public void init(Rect bounds, float startFrame, float endFrame, float frameRate, this.layerMap = layerMap; this.precomps = precomps; this.images = images; + this.imagesDpScale = imagesDpScale; this.characters = characters; this.fonts = fonts; this.markers = markers; diff --git a/lottie/src/main/java/com/airbnb/lottie/parser/LottieCompositionMoshiParser.java b/lottie/src/main/java/com/airbnb/lottie/parser/LottieCompositionMoshiParser.java index 4950753ff0..ee972df675 100644 --- a/lottie/src/main/java/com/airbnb/lottie/parser/LottieCompositionMoshiParser.java +++ b/lottie/src/main/java/com/airbnb/lottie/parser/LottieCompositionMoshiParser.java @@ -107,7 +107,7 @@ public static LottieComposition parse(JsonReader reader) throws IOException { Rect bounds = new Rect(0, 0, scaledWidth, scaledHeight); composition.init(bounds, startFrame, endFrame, frameRate, layers, layerMap, precomps, - images, characters, fonts, markers); + images, Utils.dpScale(), characters, fonts, markers); return composition; } diff --git a/lottie/src/test/java/com/airbnb/lottie/LottieDrawableTest.java b/lottie/src/test/java/com/airbnb/lottie/LottieDrawableTest.java index 0132d8d5a4..3e158ec722 100644 --- a/lottie/src/test/java/com/airbnb/lottie/LottieDrawableTest.java +++ b/lottie/src/test/java/com/airbnb/lottie/LottieDrawableTest.java @@ -36,10 +36,10 @@ public void setup() { @SuppressWarnings("SameParameterValue") private LottieComposition createComposition(int startFrame, int endFrame) { LottieComposition composition = new LottieComposition(); - composition.init(new Rect(), startFrame, endFrame, 1000, new ArrayList(), - new LongSparseArray(0), new HashMap>(0), - new HashMap(0), new SparseArrayCompat(0), - new HashMap(0), new ArrayList()); + composition.init(new Rect(), startFrame, endFrame, 1000, new ArrayList<>(), + new LongSparseArray<>(0), new HashMap<>(0), + new HashMap<>(0), 1f, new SparseArrayCompat<>(0), + new HashMap<>(0), new ArrayList<>()); return composition; } diff --git a/lottie/src/test/java/com/airbnb/lottie/LottieValueAnimatorUnitTest.java b/lottie/src/test/java/com/airbnb/lottie/LottieValueAnimatorUnitTest.java index 08b0b8c18a..102087b636 100644 --- a/lottie/src/test/java/com/airbnb/lottie/LottieValueAnimatorUnitTest.java +++ b/lottie/src/test/java/com/airbnb/lottie/LottieValueAnimatorUnitTest.java @@ -60,10 +60,10 @@ private LottieValueAnimator createAnimator() { private LottieComposition createComposition(int startFrame, int endFrame) { LottieComposition composition = new LottieComposition(); - composition.init(new Rect(), startFrame, endFrame, 1000, new ArrayList(), - new LongSparseArray(0), new HashMap>(0), - new HashMap(0), new SparseArrayCompat(0), - new HashMap(0), new ArrayList()); + composition.init(new Rect(), startFrame, endFrame, 1000, new ArrayList<>(), + new LongSparseArray<>(0), new HashMap<>(0), + new HashMap<>(0), 1f, new SparseArrayCompat<>(0), + new HashMap<>(0), new ArrayList<>()); return composition; }