【问题标题】:CameraX: How to add pinch to zoom AND tap to focus? onClickListener and onTouchListenerCameraX:如何添加捏合缩放和点击对焦? onClickListener 和 onTouchListener
【发布时间】:2020-08-01 05:49:02
【问题描述】:

我合并了一些 CameraX 教程,使其具有捏合缩放和点击对焦。 它们本身运行良好,但在一起时,OnClickListener 和 OnTouchListener 会相互干扰。

我想将它们合并到一个 OnClickListener 方法下,在 ACTION_DOWN 上执行捏合缩放,并在 ACTION_UP 上执行点击以聚焦,但只有点击聚焦在运行。即使它确实有效,但感觉有点笨拙,我会很感激一些更高级的指导。

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.
        }

    }

【问题讨论】:

    标签: android kotlin onclicklistener ontouchlistener android-camerax


    【解决方案1】:

    使用它来添加捏合缩放和点按一起聚焦:

    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
        }
    }
    

    【讨论】:

    • 我应该在哪里调用这个函数?
    • @Tarkik 在cameraProvider.bindToLifecycle()之后调用它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 2012-07-19
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    相关资源
    最近更新 更多