Skip to content

Instantly share code, notes, and snippets.

@husaynhakeem
Last active July 19, 2022 16:46
Show Gist options
  • Save husaynhakeem/d7ee8b0ec5011e11a1339e279c55fd65 to your computer and use it in GitHub Desktop.
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
}
@iqbalprabu
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment