-
-
Save husaynhakeem/d7ee8b0ec5011e11a1339e279c55fd65 to your computer and use it in GitHub Desktop.
// Listen to pinch gestures | |
val listener = object : ScaleGestureDetector.SimpleOnScaleGestureListener() { | |
override fun onScale(detector: ScaleGestureDetector): Boolean { | |
// Get the camera's current zoom ratio | |
val currentZoomRatio = cameraInfo.zoomState.value?.zoomRatio ?: 0F | |
// Get the pinch gesture's scaling factor | |
val delta = detector.scaleFactor | |
// Update the camera's zoom ratio. This is an asynchronous operation that returns | |
// a ListenableFuture, allowing you to listen to when the operation completes. | |
cameraControl.setZoomRatio(currentZoomRatio * delta) | |
// Return true, as the event was handled | |
return true | |
} | |
} | |
val scaleGestureDetector = ScaleGestureDetector(context, listener) | |
// Attach the pinch gesture listener to the viewfinder | |
previewView.setOnTouchListener { _, event -> | |
scaleGestureDetector.onTouchEvent(event) | |
return@setOnTouchListener true | |
} |
cant find zoomRatio
def camerax_version = "1.0.0-alpha05"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"using this as per codelab
use like this.
camera?.cameraInfo?.zoomState?.value?.zoomRatio
As it is mentioned in the source comments the method
cameraControl.setZoomRatio(currentZoomRatio * delta)
seems to by asnc, so in case of multiple "onScale" events in a short period of time, the value read from
cameraInfo.zoomState.value?.zoomRatio
might not yet have been updated. In my case this leads to the zoom jumping back and forth during pinching.
Is there any way to work around this?
i found this error, can you help me ?
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.view.ScaleGestureDetector.(ScaleGestureDetector.java:237)
at android.view.ScaleGestureDetector.(ScaleGestureDetector.java:217)
at id.t_rec.app.modules.absentx.AbsentXActivity.startCamera(AbsentXActivity.kt:126)
cant find zoomRatio
def camerax_version = "1.0.0-alpha05"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
using this as per codelab