android / camera-samples

Multiple samples showing the best practices in camera APIs on Android.
Apache License 2.0
4.99k stars 2.34k forks source link

CameraXBasic: Update sample with latest stable WindowManager #480

Open zeusalmighty717 opened 2 years ago

zeusalmighty717 commented 2 years ago

When reporting the issue latest WindowManager available is androidx.window:window:1.0.0

mpicci commented 2 years ago

I updated the latest WindowManager but I get: CameraXBasic/app/src/main/java/com/android/example/cameraxbasic/fragments/CameraFragment.kt: (49, 24): Unresolved reference: WindowManager CameraXBasic/app/src/main/java/com/android/example/cameraxbasic/fragments/CameraFragment.kt: (102, 41): Unresolved reference: WindowManager CameraXBasic/app/src/main/java/com/android/example/cameraxbasic/fragments/CameraFragment.kt: (205, 25): Unresolved reference: WindowManager

syslogic commented 2 years ago

class androidx.window.WindowManager might have been removed to avoid confusion with interface android.view.WindowManager.

The suggested fix would be to obtain the aspect ratio with the WindowMetricsCalculator:

// Get screen metrics used to setup camera for full screen resolution
val windowMetrics = WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(requireActivity())
Log.d(TAG, "Screen metrics: ${windowMetrics.bounds.width()} x ${windowMetrics.bounds.height()}")

val screenAspectRatio = aspectRatio(windowMetrics.bounds.width(), windowMetrics.bounds.height())
Log.d(TAG, "Preview aspect ratio: $screenAspectRatio")

See this blog post: Unbundling the WindowManager.