davidliu
Committed by GitHub

Cache target zoom value for more accuracy while zooming (#426)

* Cache target zoom value for more accuracy while zooming

* spotless
@@ -14,6 +14,8 @@ @@ -14,6 +14,8 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
  17 +@file:Suppress("unused")
  18 +
17 package io.livekit.android.camerax.ui 19 package io.livekit.android.camerax.ui
18 20
19 import android.content.Context 21 import android.content.Context
@@ -69,6 +71,8 @@ class ScaleZoomHelper( @@ -69,6 +71,8 @@ class ScaleZoomHelper(
69 } 71 }
70 } 72 }
71 73
  74 + private var targetZoom: Float? = null
  75 +
72 /** 76 /**
73 * Scales the current zoom value by [factor]. 77 * Scales the current zoom value by [factor].
74 * 78 *
@@ -78,14 +82,25 @@ class ScaleZoomHelper( @@ -78,14 +82,25 @@ class ScaleZoomHelper(
78 * @see CameraControl.setZoomRatio 82 * @see CameraControl.setZoomRatio
79 * @see createGestureDetector 83 * @see createGestureDetector
80 */ 84 */
  85 + @Synchronized
81 fun zoom(factor: Float) { 86 fun zoom(factor: Float) {
82 val camera = cameraFlow?.value ?: return 87 val camera = cameraFlow?.value ?: return
83 val zoomState = camera.cameraInfo.zoomState.value ?: return 88 val zoomState = camera.cameraInfo.zoomState.value ?: return
84 - val currentZoom = zoomState.zoomRatio 89 +
  90 + val currentZoom = targetZoom ?: zoomState.zoomRatio
85 val newZoom = (currentZoom * factor).coerceIn(zoomState.minZoomRatio, zoomState.maxZoomRatio) 91 val newZoom = (currentZoom * factor).coerceIn(zoomState.minZoomRatio, zoomState.maxZoomRatio)
86 92
87 if (newZoom != currentZoom) { 93 if (newZoom != currentZoom) {
  94 + targetZoom = newZoom
88 camera.cameraControl.setZoomRatio(newZoom) 95 camera.cameraControl.setZoomRatio(newZoom)
  96 + .addListener(
  97 + {
  98 + synchronized(this) {
  99 + targetZoom = null
  100 + }
  101 + },
  102 + { it.run() },
  103 + )
89 } 104 }
90 } 105 }
91 106