简体   繁体   English

CameraX:如何添加捏合缩放和点击对焦? onClickListener 和 onTouchListener

[英]CameraX: How to add pinch to zoom AND tap to focus? onClickListener and onTouchListener

I merged some CameraX tutorials so that it has pinch to zoom and tap to focus.我合并了一些 CameraX 教程,以便它可以缩放和点击聚焦。 By themselves, they work well, but together, the OnClickListener and OnTouchListeners are interfering with each other.它们本身工作得很好,但是 OnClickListener 和 OnTouchListener 在一起会相互干扰。

I thought merging them under a single OnClickListener method where the pinch to zoom is executed on the ACTION_DOWN, and the tap to focus on the ACTION_UP, but only the tap to focus is running.我想将它们合并到一个 OnClickListener 方法下,在 ACTION_DOWN 上执行捏合缩放,点击聚焦在 ACTION_UP 上,但只有点击聚焦在运行。 Even if it did work, this feels a bit clunky and I'd appreciate some more advanced guidance.即使它确实有效,这也感觉有点笨拙,我会很感激一些更高级的指导。

zoomAndFocus is triggered by: "viewFinder.setOnClickListener{zoomAndFocus()}" in onCreate. zoomAndFocus 由 onCreate 中的“viewFinder.setOnClickListener{zoomAndFocus()}”触发。

private fun zoomAndFocus(){
    Log.d("onclick","detecting clck?")

    viewFinder.setOnTouchListener { _, event ->

        return@setOnTouchListener when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                val listener = object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
                    override fun onScale(detector: ScaleGestureDetector): Boolean {
                        val zoomRatio = camera?.cameraInfo?.zoomState?.value?.zoomRatio ?: 0f
                        val scale = zoomRatio * detector.scaleFactor
                        camera!!.cameraControl.setZoomRatio(scale)
                        return true
                    }
                }

                val scaleGestureDetector = ScaleGestureDetector(this, listener)

                scaleGestureDetector.onTouchEvent(event)


                true
            }
            MotionEvent.ACTION_UP -> {
                val factory: MeteringPointFactory = SurfaceOrientedMeteringPointFactory(
                    viewFinder.width.toFloat(), viewFinder.height.toFloat()
                )
                val autoFocusPoint = factory.createPoint(event.x, event.y)
                try {
                    camera?.cameraControl?.startFocusAndMetering(
                        FocusMeteringAction.Builder(
                            autoFocusPoint,
                            FocusMeteringAction.FLAG_AF
                        ).apply {
                            //focus only when the user tap the preview
                            disableAutoCancel()
                        }.build()
                    )
                } catch (e: CameraInfoUnavailableException) {
                    Log.d("ERROR", "cannot access camera", e)
                }
                viewFinder.performClick()
                true
            }
            else -> false // Unhandled event.
        }

    }

Use this to add pinch to zoom and tap to focus together:使用它来添加捏缩放和点击一起聚焦:

private fun setupZoomAndTapToFocus() {
    val listener = object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
        override fun onScale(detector: ScaleGestureDetector): Boolean {
            val currentZoomRatio: Float = cameraInfo.zoomState.value?.zoomRatio ?: 1F
            val delta = detector.scaleFactor
            cameraControl.setZoomRatio(currentZoomRatio * delta)
            return true
        }
    }

    val scaleGestureDetector = ScaleGestureDetector(viewFinder.context, listener)

    viewFinder.setOnTouchListener { _, event ->
        scaleGestureDetector.onTouchEvent(event)
        if (event.action == MotionEvent.ACTION_DOWN) {
            val factory = viewFinder.createMeteringPointFactory(cameraSelector)
            val point = factory.createPoint(event.x, event.y)
            val action = FocusMeteringAction.Builder(point, FocusMeteringAction.FLAG_AF)
                .setAutoCancelDuration(5, TimeUnit.SECONDS)
                .build()
            cameraControl.startFocusAndMetering(action)
        }
        true
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM