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

LottieComposition: Add getUnscaledHeight & getUnscaledWidth functions. #2514

Merged
merged 1 commit into from
Aug 1, 2024

Conversation

vanniktech
Copy link
Contributor

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.

Copy link
Collaborator

@gpeal gpeal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you divide the returned width/height by Resources.getSystem().getDisplayMetrics().density instead? That's all the scaling is doing.

@vanniktech
Copy link
Contributor Author

I also thought about that. But then I'd kind of rely on the implementation that's inside Lottie. If that changes for some reason, I'm screwed. iOS also offers to get the original size:

Screenshot 2024-06-29 at 14 01 07

Also after dividing, I'd need to round it (I want the integer value).

@@ -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, Utils.dpScale(), characters, fonts, markers);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I do pass the unscaled width & height here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make the names very clearly named unscaledWidth and unscaledHeight?

@gpeal
Copy link
Collaborator

gpeal commented Jun 29, 2024

Can you divide the returned width/height by Resources.getSystem().getDisplayMetrics().density instead? That's all the scaling is doing.

The implementation of that definitely won't change. I'd like to keep the API surface area of Lottie as small as possible. If the difference here really is dividing by that number, I would prefer not to add it to the core library.

@vanniktech
Copy link
Contributor Author

The difference is by dividing that number and needing to round it which may yield 399 instead of the original 400 depending on the scale and the rounding mode. It's really not a lot that is added to the API surface, has parity with the iOS implementation and yields something that is part of the format.

Copy link

github-actions bot commented Jul 2, 2024

Snapshot Tests
API 23: Report Diff
API 31: Report Diff

Copy link
Collaborator

@gpeal gpeal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean with regards to potentially being off by one due to multiplying a rounded value.

Depending on your use case, you could create a JsonReader for the same animation file and read the h and h top-level properties yourself.

We can add this to the core library but let's make the API naming very explicit.

@@ -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, Utils.dpScale(), characters, fonts, markers);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make the names very clearly named unscaledWidth and unscaledHeight?

@vanniktech vanniktech force-pushed the lottie-composition-width-height branch from a08adc3 to dc5fbda Compare July 2, 2024 07:39
@vanniktech
Copy link
Contributor Author

Do you also want me to prefix the exposed API with unscaled?

@gpeal
Copy link
Collaborator

gpeal commented Jul 2, 2024

Do you also want me to prefix the exposed API with unscaled?

Yes, please!

@vanniktech vanniktech force-pushed the lottie-composition-width-height branch from dc5fbda to 86a2bad Compare July 3, 2024 02:24
@vanniktech vanniktech changed the title LottieComposition: Add getHeight & getWidth function to return original dimensions. LottieComposition: Add getUnscaledHeight & getUnscaledWidth functions. Jul 3, 2024
@vanniktech vanniktech force-pushed the lottie-composition-width-height branch from 86a2bad to 79520d3 Compare July 3, 2024 02:25
@vanniktech
Copy link
Contributor Author

Should be good to go now.

Copy link

github-actions bot commented Aug 1, 2024

Snapshot Tests
API 23: Report Diff
API 31: Report Diff

@gpeal gpeal merged commit 3f39884 into airbnb:master Aug 1, 2024
7 checks passed
@vanniktech vanniktech deleted the lottie-composition-width-height branch August 2, 2024 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants