Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scale image if the dp scale changes #2438

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ class LottieClipSpecTest {
LongSparseArray(),
emptyMap(),
emptyMap(),
1f,
SparseArrayCompat(),
emptyMap(),
markers,
)
return composition
}
}
}
22 changes: 20 additions & 2 deletions 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;
/**
* Map of font names to fonts
*/
Expand Down Expand Up @@ -71,7 +75,7 @@ public class LottieComposition {
@RestrictTo(RestrictTo.Scope.LIBRARY)
public void init(Rect bounds, float startFrame, float endFrame, float frameRate,
List<Layer> layers, LongSparseArray<Layer> layerMap, Map<String,
List<Layer>> precomps, Map<String, LottieImageAsset> images,
List<Layer>> precomps, Map<String, LottieImageAsset> images, float imagesDpScale,
SparseArrayCompat<FontCharacter> characters, Map<String, Font> fonts,
List<Marker> markers) {
this.bounds = bounds;
Expand All @@ -82,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;
Expand Down Expand Up @@ -208,8 +213,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 +253,7 @@ public String toString() {
*/
@Deprecated
public static class Factory {

private Factory() {
}

Expand Down Expand Up @@ -363,6 +380,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 +400,4 @@ private ListenerAdapter(OnCompositionLoadedListener listener) {
}
}
}
}
}
13 changes: 13 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,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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Layer>(),
new LongSparseArray<Layer>(0), new HashMap<String, List<Layer>>(0),
new HashMap<String, LottieImageAsset>(0), new SparseArrayCompat<FontCharacter>(0),
new HashMap<String, Font>(0), new ArrayList<Marker>());
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Layer>(),
new LongSparseArray<Layer>(0), new HashMap<String, List<Layer>>(0),
new HashMap<String, LottieImageAsset>(0), new SparseArrayCompat<FontCharacter>(0),
new HashMap<String, Font>(0), new ArrayList<Marker>());
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;
}

Expand Down
Loading