Skip to content

Commit

Permalink
LottieComposition: Add getUnscaledHeight & getUnscaledWidth functions. (
Browse files Browse the repository at this point in the history
#2514)

Use case: I just want to know the unscaled original height / width of the LottieAnimation. I want to do my own scaling by calculating for instance given an aspect ratio or weighted settings, how big/small I can stretch the animation.
  • Loading branch information
vanniktech authored Aug 1, 2024
1 parent 71c1622 commit 3f39884
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class LottieClipSpecTest {
SparseArrayCompat(),
emptyMap(),
markers,
0,
0,
)
return composition
}
Expand Down
14 changes: 13 additions & 1 deletion lottie/src/main/java/com/airbnb/lottie/LottieComposition.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,15 @@ public class LottieComposition {
*/
private int maskAndMatteCount = 0;

private int unscaledWidth;
private int unscaledHeight;

@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, float imagesDpScale,
SparseArrayCompat<FontCharacter> characters, Map<String, Font> fonts,
List<Marker> markers) {
List<Marker> markers, int unscaledWidth, int unscaledHeight) {
this.bounds = bounds;
this.startFrame = startFrame;
this.endFrame = endFrame;
Expand All @@ -89,6 +92,8 @@ public void init(Rect bounds, float startFrame, float endFrame, float frameRate,
this.characters = characters;
this.fonts = fonts;
this.markers = markers;
this.unscaledWidth = unscaledWidth;
this.unscaledHeight = unscaledHeight;
}

@RestrictTo(RestrictTo.Scope.LIBRARY)
Expand Down Expand Up @@ -232,6 +237,13 @@ public float getDurationFrames() {
return endFrame - startFrame;
}

public int getUnscaledWidth() {
return unscaledWidth;
}

public int getUnscaledHeight() {
return unscaledHeight;
}

@NonNull
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public static LottieComposition parse(JsonReader reader) throws IOException {
float frameRate = 0f;
final LongSparseArray<Layer> layerMap = new LongSparseArray<>();
final List<Layer> layers = new ArrayList<>();
int width = 0;
int height = 0;
int unscaledWidth = 0;
int unscaledHeight = 0;
Map<String, List<Layer>> precomps = new HashMap<>();
Map<String, LottieImageAsset> images = new HashMap<>();
Map<String, Font> fonts = new HashMap<>();
Expand All @@ -57,10 +57,10 @@ public static LottieComposition parse(JsonReader reader) throws IOException {
while (reader.hasNext()) {
switch (reader.selectName(NAMES)) {
case 0:
width = reader.nextInt();
unscaledWidth = reader.nextInt();
break;
case 1:
height = reader.nextInt();
unscaledHeight = reader.nextInt();
break;
case 2:
startFrame = (float) reader.nextDouble();
Expand Down Expand Up @@ -102,12 +102,12 @@ public static LottieComposition parse(JsonReader reader) throws IOException {
reader.skipValue();
}
}
int scaledWidth = (int) (width * scale);
int scaledHeight = (int) (height * scale);
int scaledWidth = (int) (unscaledWidth * scale);
int scaledHeight = (int) (unscaledHeight * scale);
Rect bounds = new Rect(0, 0, scaledWidth, scaledHeight);

composition.init(bounds, startFrame, endFrame, frameRate, layers, layerMap, precomps,
images, Utils.dpScale(), characters, fonts, markers);
images, Utils.dpScale(), characters, fonts, markers, unscaledWidth, unscaledHeight);

return composition;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import android.graphics.Rect;
import androidx.collection.LongSparseArray;
import androidx.collection.SparseArrayCompat;
import com.airbnb.lottie.model.Font;
import com.airbnb.lottie.model.FontCharacter;
import com.airbnb.lottie.model.Marker;
import com.airbnb.lottie.model.layer.Layer;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -16,7 +12,6 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static junit.framework.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -39,7 +34,7 @@ private LottieComposition createComposition(int startFrame, int endFrame) {
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<>());
new HashMap<>(0), new ArrayList<>(), 0, 0);
return composition;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import android.graphics.Rect;
import androidx.collection.LongSparseArray;
import androidx.collection.SparseArrayCompat;
import com.airbnb.lottie.model.Font;
import com.airbnb.lottie.model.FontCharacter;
import com.airbnb.lottie.model.Marker;
import com.airbnb.lottie.model.layer.Layer;
import com.airbnb.lottie.utils.LottieValueAnimator;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -17,7 +13,6 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import static junit.framework.Assert.assertEquals;
Expand Down Expand Up @@ -63,7 +58,7 @@ private LottieComposition createComposition(int startFrame, int endFrame) {
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<>());
new HashMap<>(0), new ArrayList<>(), 0, 0);
return composition;
}

Expand Down

0 comments on commit 3f39884

Please sign in to comment.