Skip to content

Commit

Permalink
Scale image if the dp scale changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gpeal committed Dec 29, 2023
1 parent a855905 commit ee4d9e9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lottie/src/main/java/com/airbnb/lottie/LottieComposition.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,6 +47,7 @@ public class LottieComposition {
private final HashSet<String> warnings = new HashSet<>();
private Map<String, List<Layer>> precomps;
private Map<String, LottieImageAsset> images;
private float imagesDpScale = Utils.dpScale();
/**
* Map of font names to fonts
*/
Expand Down Expand Up @@ -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<String, LottieImageAsset> getImages() {
float dpScale = Utils.dpScale();
if (dpScale != imagesDpScale) {
imagesDpScale = dpScale;
Set<Map.Entry<String, LottieImageAsset>> entries = images.entrySet();

for (Map.Entry<String, LottieImageAsset> entry : entries) {
images.put(entry.getKey(), entry.getValue().copyWithScale(imagesDpScale / dpScale));
}
}
return images;
}

Expand Down Expand Up @@ -237,6 +252,7 @@ public String toString() {
*/
@Deprecated
public static class Factory {

private Factory() {
}

Expand Down Expand Up @@ -363,6 +379,7 @@ public static LottieComposition fromJsonSync(JsonReader reader) {

@SuppressWarnings("deprecation")
private static final class ListenerAdapter implements LottieListener<LottieComposition>, Cancellable {

private final OnCompositionLoadedListener listener;
private boolean cancelled = false;

Expand All @@ -382,4 +399,4 @@ private ListenerAdapter(OnCompositionLoadedListener listener) {
}
}
}
}
}
15 changes: 15 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/LottieImageAsset.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ 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) {
if (bitmap == null) {
return new LottieImageAsset(width, height, id, fileName, dirName);
}
// scale bitmap by scale
LottieImageAsset newAsset = new LottieImageAsset((int) (width * scale), (int) (height * scale), id, fileName, dirName);
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.
*/
Expand Down

0 comments on commit ee4d9e9

Please sign in to comment.