Repository navigation
Fix ViewPort and PreviewDisplay aspect ratio on landscape-natural devices #592
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -667,9 +667,17 @@ internal fun createUseCaseGroup( | |
| TAG, | ||
| "Setting initial device rotation to ${initialTransientSettings.deviceRotation}" | ||
| ) | ||
| val isSensorRotationLandscape = | ||
| (cameraInfo.getSensorRotationDegrees(previewUseCase.targetRotation) % 180 == 0) == | ||
| (cameraInfo.sensorRect.width() >= cameraInfo.sensorRect.height()) | ||
| val viewPortAspectRatio = if (isSensorRotationLandscape) { | ||
| Rational(aspectRatio.denominator, aspectRatio.numerator) | ||
| } else { | ||
| Rational(aspectRatio.numerator, aspectRatio.denominator) | ||
| } | ||
|
Comment on lines
+670
to
+677
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new viewport aspect ratio calculation logic for landscape-natural devices is a critical change that should be covered by unit tests to prevent future regressions. Please add a unit test (e.g., in For example, the test should verify:
Ensure the test uses Robolectric and Truth assertions ( |
||
| setViewPort( | ||
| ViewPort.Builder( | ||
| Rational(aspectRatio.numerator, aspectRatio.denominator), | ||
| viewPortAspectRatio, | ||
| // Initialize rotation to Preview's rotation, which comes from Display rotation | ||
| previewUseCase.targetRotation | ||
| ).build() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The formula used to determine if the sensor output is landscape is highly elegant but non-obvious. To ensure long-term maintainability and prevent future developers from accidentally breaking or misunderstanding this logic, please add a brief explanatory comment explaining how the relative rotation and sensor dimensions are used to compute the final viewport orientation.
References