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 getHeight & getWidth function to return original dimensions. #2514

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vanniktech
Copy link

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
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
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
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
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!

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.

None yet

2 participants