# ZoomGestureDetector

Artifact: [androidx.camera.viewfinder:viewfinder-core](https://developer.android.com/jetpack/androidx/releases/camera-viewfinder) [View Source](https://cs.android.com/search?q=file:androidx/camera/viewfinder/core/ZoomGestureDetector.kt+class:androidx.camera.viewfinder.core.ZoomGestureDetector) Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/camera-viewfinder#1.5.0)

*** ** * ** ***


```
public final class ZoomGestureDetector
```

<br />

*** ** * ** ***

Detector that interprets `https://developer.android.com/reference/android/view/MotionEvent.html`s and notify users when a zooming gesture has occurred.

To use this class to do pinch-to-zoom on the viewfinder:

- In the `https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector.OnZoomGestureListener#onZoomEvent(androidx.camera.viewfinder.core.ZoomGestureDetector.ZoomEvent)`, when receiving `https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector.ZoomEvent.Move`, get the `https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector.ZoomEvent.Move#incrementalScaleFactor()` and use the value with `CameraControl.setZoomRatio` if the factor is in the range of `ZoomState.getMinZoomRatio` and `ZoomState.getMaxZoomRatio`. Then create an instance of the `ZoomGestureDetector` with the `https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector.OnZoomGestureListener`.

- In the `https://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent)`, call `https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector#onTouchEvent(android.view.MotionEvent)` and pass the `https://developer.android.com/reference/android/view/MotionEvent.html`s to the `ZoomGestureDetector`.

```kotlin
import androidx.camera.viewfinder.core.ZoomGestureDetector
import androidx.camera.viewfinder.core.ZoomGestureDetector.ZoomEvent

val zoomGestureDetector =
    ZoomGestureDetector(context) { zoomEvent ->
        when (zoomEvent) {
            is ZoomEvent.Move -> {
                val zoomState = camera.cameraInfo.zoomState.value!!
                val ratio = zoomState.zoomRatio * zoomEvent.incrementalScaleFactor
                val minRatio = zoomState.minZoomRatio
                val maxRatio = zoomState.maxZoomRatio
                val clampedRatio = min(max(ratio, minRatio), maxRatio)
                camera.cameraControl.setZoomRatio(clampedRatio)
            }
            is ZoomEvent.Begin -> {
                // Handle the begin event. For example, determine whether this gesture
                // should be processed further.
            }
            is ZoomEvent.End -> {
                // Handle the end event. For example, show a UI indicator.
            }
        }
        true
    }

return zoomGestureDetector.onTouchEvent(event)
```

## Summary

| ### Nested types |
|---|
| `public fun interface https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector.OnZoomGestureListener` The listener for receiving notifications when gestures occur. |
| `public abstract class https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector.ZoomEvent` The zoom event that contains extended info about event state. |
| `public final class https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector.ZoomEvent.Begin extends https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector.ZoomEvent` The beginning of a zoom gesture. |
| `public final class https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector.ZoomEvent.End extends https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector.ZoomEvent` The end of a zoom gesture. |
| `public final class https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector.ZoomEvent.Move extends https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector.ZoomEvent` The moving events of a gesture in progress. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector#ZoomGestureDetector(android.content.Context,kotlin.Int,kotlin.Int,androidx.camera.viewfinder.core.ZoomGestureDetector.OnZoomGestureListener)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context, @https://developer.android.com/reference/androidx/annotation/Px int spanSlop, @https://developer.android.com/reference/androidx/annotation/Px int minSpan, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector.OnZoomGestureListener listener )` Creates a ZoomGestureDetector for detecting zooming gesture. |

| ### Public methods |
|---|---|
| `final long` | `https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector#getTimeDelta()()` Returns the time difference in milliseconds between the previous accepted zooming event and the current zooming event. |
| `final boolean` | `https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector#isQuickZoomEnabled()()` Whether the quick zoom gesture, in which the user performs a double tap followed by a swipe, should perform zooming. |
| `final boolean` | `https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector#isStylusZoomEnabled()()` Whether the stylus zoom gesture, in which the user uses a stylus and presses the button, should perform zooming. |
| `final boolean` | `@https://developer.android.com/reference/androidx/annotation/UiThread https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector#onTouchEvent(android.view.MotionEvent)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/MotionEvent.html event)` Accepts `https://developer.android.com/reference/android/view/MotionEvent.html`s and dispatches events to a `https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector.OnZoomGestureListener` when appropriate. |
| `final void` | `https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector#setQuickZoomEnabled(kotlin.Boolean)(boolean value)` Whether the quick zoom gesture, in which the user performs a double tap followed by a swipe, should perform zooming. |
| `final void` | `https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector#setStylusZoomEnabled(kotlin.Boolean)(boolean value)` Whether the stylus zoom gesture, in which the user uses a stylus and presses the button, should perform zooming. |

## Public constructors

### ZoomGestureDetector

Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/camera-viewfinder#1.5.0)

```
public ZoomGestureDetector(
    @NonNull Context context,
    @Px int spanSlop,
    @Px int minSpan,
    @NonNull ZoomGestureDetector.OnZoomGestureListener listener
)
```

Creates a ZoomGestureDetector for detecting zooming gesture.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context` | The application context. |
| `@https://developer.android.com/reference/androidx/annotation/Px int spanSlop` | The distance in pixels touches can wander before a gesture to be interpreted as zooming. |
| `@https://developer.android.com/reference/androidx/annotation/Px int minSpan` | The distance in pixels between touches that must be reached for a gesture to be interpreted as zooming. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector.OnZoomGestureListener listener` | The listener to receive the callback. |

## Public methods

### getTimeDelta

Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/camera-viewfinder#1.5.0)

```
public final long getTimeDelta()
```

Returns the time difference in milliseconds between the previous accepted zooming event and the current zooming event.

| Returns |
|---|---|
| `long` | Time difference since the last zooming event in milliseconds. |

### isQuickZoomEnabled

Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/camera-viewfinder#1.5.0)

```
public final boolean isQuickZoomEnabled()
```

Whether the quick zoom gesture, in which the user performs a double tap followed by a swipe, should perform zooming.

If not set, this is enabled by default.

### isStylusZoomEnabled

Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/camera-viewfinder#1.5.0)

```
public final boolean isStylusZoomEnabled()
```

Whether the stylus zoom gesture, in which the user uses a stylus and presses the button, should perform zooming.

If not set, this is enabled by default.

### onTouchEvent

Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/camera-viewfinder#1.5.0)

```
@UiThread
public final boolean onTouchEvent(@NonNull MotionEvent event)
```

Accepts `https://developer.android.com/reference/android/view/MotionEvent.html`s and dispatches events to a `https://developer.android.com/reference/androidx/camera/viewfinder/core/ZoomGestureDetector.OnZoomGestureListener` when appropriate.

Applications should pass a complete and consistent event stream to this method.

A complete and consistent event stream involves all `https://developer.android.com/reference/android/view/MotionEvent.html`s from the initial `https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_DOWN` to the final `https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_UP` or `https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_CANCEL`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/MotionEvent.html event` | The event to process. |

| Returns |
|---|---|
| `boolean` | `true` if the event was processed and the detector wants to receive the rest of the `https://developer.android.com/reference/android/view/MotionEvent.html`s in this event stream. Return it in the `https://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent)` for a normal use case. |

### setQuickZoomEnabled

Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/camera-viewfinder#1.5.0)

```
public final void setQuickZoomEnabled(boolean value)
```

Whether the quick zoom gesture, in which the user performs a double tap followed by a swipe, should perform zooming.

If not set, this is enabled by default.

### setStylusZoomEnabled

Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/camera-viewfinder#1.5.0)

```
public final void setStylusZoomEnabled(boolean value)
```

Whether the stylus zoom gesture, in which the user uses a stylus and presses the button, should perform zooming.

If not set, this is enabled by default.