fix: Apply correct initial rotation on Android (#368)

* fix: Apply correct initial rotation

* Make `rotation` a property getter

* Update CameraView.kt
This commit is contained in:
Marc Rousavy 2021-08-25 11:16:07 +02:00 committed by GitHub
parent f3e1df009e
commit c7fb89170e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 15 deletions

View File

@ -109,12 +109,14 @@ class CameraView(context: Context) : FrameLayout(context), LifecycleOwner {
private val scaleGestureListener: ScaleGestureDetector.SimpleOnScaleGestureListener private val scaleGestureListener: ScaleGestureDetector.SimpleOnScaleGestureListener
private val scaleGestureDetector: ScaleGestureDetector private val scaleGestureDetector: ScaleGestureDetector
private val touchEventListener: OnTouchListener private val touchEventListener: OnTouchListener
private val orientationEventListener: OrientationEventListener
private val lifecycleRegistry: LifecycleRegistry private val lifecycleRegistry: LifecycleRegistry
private var hostLifecycleState: Lifecycle.State private var hostLifecycleState: Lifecycle.State
private var rotation: Int = Surface.ROTATION_0 private val rotation: Int
get() {
return context.displayRotation
}
private var minZoom: Float = 1f private var minZoom: Float = 1f
private var maxZoom: Float = 1f private var maxZoom: Float = 1f
@ -169,17 +171,7 @@ class CameraView(context: Context) : FrameLayout(context), LifecycleOwner {
} }
scaleGestureDetector = ScaleGestureDetector(context, scaleGestureListener) scaleGestureDetector = ScaleGestureDetector(context, scaleGestureListener)
touchEventListener = OnTouchListener { _, event -> return@OnTouchListener scaleGestureDetector.onTouchEvent(event) } touchEventListener = OnTouchListener { _, event -> return@OnTouchListener scaleGestureDetector.onTouchEvent(event) }
orientationEventListener = object : OrientationEventListener(context) {
override fun onOrientationChanged(orientation : Int) {
rotation = when (orientation) {
in 45..134 -> Surface.ROTATION_270
in 135..224 -> Surface.ROTATION_180
in 225..314 -> Surface.ROTATION_90
else -> Surface.ROTATION_0
}
}
}
orientationEventListener.enable()
hostLifecycleState = Lifecycle.State.INITIALIZED hostLifecycleState = Lifecycle.State.INITIALIZED
lifecycleRegistry = LifecycleRegistry(this) lifecycleRegistry = LifecycleRegistry(this)
@ -216,7 +208,6 @@ class CameraView(context: Context) : FrameLayout(context), LifecycleOwner {
fun finalize() { fun finalize() {
mHybridData.resetNative() mHybridData.resetNative()
orientationEventListener.disable()
} }
private external fun initHybrid(): HybridData private external fun initHybrid(): HybridData
@ -275,7 +266,7 @@ class CameraView(context: Context) : FrameLayout(context), LifecycleOwner {
configureSession() configureSession()
} }
if (shouldReconfigureZoom) { if (shouldReconfigureZoom) {
val zoomClamped = max(min(zoom.toFloat(), maxZoom), minZoom) val zoomClamped = max(min(zoom, maxZoom), minZoom)
camera!!.cameraControl.setZoomRatio(zoomClamped) camera!!.cameraControl.setZoomRatio(zoomClamped)
} }
if (shouldReconfigureTorch) { if (shouldReconfigureTorch) {

View File

@ -0,0 +1,36 @@
package com.mrousavy.camera.utils
import android.content.Context
import android.os.Build
import android.view.Surface
import android.view.WindowManager
import com.facebook.react.bridge.ReactContext
val Context.displayRotation: Int
get() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Context.display
this.display?.let { display ->
return display.rotation
}
// ReactContext.currentActivity.display
if (this is ReactContext) {
currentActivity?.display?.let { display ->
return display.rotation
}
}
}
// WindowManager.defaultDisplay
val windowManager = getSystemService(Context.WINDOW_SERVICE) as? WindowManager
if (windowManager != null) {
@Suppress("DEPRECATION") // deprecated since SDK 30
windowManager.defaultDisplay?.let { display ->
return display.rotation
}
}
// 0
return Surface.ROTATION_0
}