Forked from husaynhakeem/camerax-previewview-pinch-to-zoom.kt
Created
August 21, 2023 04:18
-
-
Save e4basil/e590b67da247884bf110cb7092b206d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a listener with a callback invoked when a gesture event has occurred | |
val listener = object : ScaleGestureDetector.SimpleOnScaleGestureListener() { | |
override fun onScale(detector: ScaleGestureDetector): Boolean { | |
// Get the current camera zoom ratio | |
val currentZoomRatio: Float = cameraInfo.zoomRatio.value ?: 1F | |
// Get by how much the scale has changed due to the user's pinch gesture | |
val delta = detector.scaleFactor | |
// Update the camera's zoom ratio | |
cameraControl.setZoomRatio(currentZoomRatio * delta) | |
return true | |
} | |
} | |
// Attach PreviewView's touch events listener to the scale gesture listener | |
val scaleGestureDetector = ScaleGestureDetector(context, listener) | |
// Pass touch events from PreviewView to the scale gesture listener | |
previewView.setOnTouchListener { _, event -> | |
scaleGestureDetector.onTouchEvent(event) | |
return@setOnTouchListener true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment