ZoomGestureDetector


@RequiresApi(value = 21)
class ZoomGestureDetector


Detector that interprets MotionEvents and notify users when a zooming gesture has occurred.

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

import androidx.camera.viewfinder.core.ZoomGestureDetector

val zoomGestureDetector = ZoomGestureDetector(context) { type, detector ->
    when (type) {
        ZoomGestureDetector.ZOOM_GESTURE_MOVE -> {
            val zoomState = camera.cameraInfo.zoomState.value!!
            val ratio = zoomState.zoomRatio * detector.scaleFactor
            val minRatio = zoomState.minZoomRatio
            val maxRatio = zoomState.maxZoomRatio
            val clampedRatio = min(max(ratio, minRatio), maxRatio)
            camera.cameraControl.setZoomRatio(clampedRatio)
        }

        ZoomGestureDetector.ZOOM_GESTURE_BEGIN -> {
            // Handle the begin event. For example, determine whether this gesture
            // should be processed further.
        }

        ZoomGestureDetector.ZOOM_GESTURE_END -> {
            // Handle the end event. For example, show a UI indicator.
        }
    }
    true
}

return zoomGestureDetector.onTouchEvent(event)

Summary

Nested types

The listener for receiving notifications when gestures occur.

Constants

const Int

The beginning of a zoom gesture.

const Int

The end of a zoom gesture.

const Int

The moving events of a gesture in progress.

Public constructors

ZoomGestureDetector(
    context: Context,
    spanSlop: Int,
    minSpan: Int,
    listener: ZoomGestureDetector.OnZoomGestureListener
)

Creates a ZoomGestureDetector for detecting zooming gesture.

Public functions

Boolean

Accepts MotionEvents and dispatches events to a OnZoomGestureListener when appropriate.

Public properties

Long

The event time in milliseconds of the current event being processed.

Float

The X coordinate of the current gesture's focal point in pixels.

Float

The Y coordinate of the current gesture's focal point in pixels.

Boolean

Whether a zoom gesture is in progress.

Boolean

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

Boolean

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

Float
Long

Constants

ZOOM_GESTURE_BEGIN

const val ZOOM_GESTURE_BEGIN = 1: Int

The beginning of a zoom gesture. Reported by new pointers going down.

ZOOM_GESTURE_END

const val ZOOM_GESTURE_END = 2: Int

The end of a zoom gesture. Reported by existing pointers going up.

ZOOM_GESTURE_MOVE

const val ZOOM_GESTURE_MOVE = 0: Int

The moving events of a gesture in progress. Reported by pointer motion.

Public constructors

ZoomGestureDetector

Added in 1.4.0-alpha05
ZoomGestureDetector(
    context: Context,
    spanSlop: Int = ViewConfiguration.get(context).scaledTouchSlop * 2,
    minSpan: Int = DEFAULT_MIN_SPAN,
    listener: ZoomGestureDetector.OnZoomGestureListener
)

Creates a ZoomGestureDetector for detecting zooming gesture.

Parameters
context: Context

The application context.

spanSlop: Int = ViewConfiguration.get(context).scaledTouchSlop * 2

The distance in pixels touches can wander before a gesture to be interpreted as zooming.

minSpan: Int = DEFAULT_MIN_SPAN

The distance in pixels between touches that must be reached for a gesture to be interpreted as zooming.

listener: ZoomGestureDetector.OnZoomGestureListener

The listener to receive the callback.

Public functions

onTouchEvent

Added in 1.4.0-alpha05
@UiThread
fun onTouchEvent(event: MotionEvent): Boolean

Accepts MotionEvents and dispatches events to a OnZoomGestureListener when appropriate.

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

A complete and consistent event stream involves all MotionEvents from the initial MotionEvent.ACTION_DOWN to the final MotionEvent.ACTION_UP or MotionEvent.ACTION_CANCEL.

Parameters
event: MotionEvent

The event to process.

Returns
Boolean

true if the event was processed and the detector wants to receive the rest of the MotionEvents in this event stream. Return it in the View.onTouchEvent for a normal use case.

Public properties

eventTime

Added in 1.4.0-alpha05
val eventTimeLong

The event time in milliseconds of the current event being processed.

focusX

Added in 1.4.0-alpha05
val focusXFloat

The X coordinate of the current gesture's focal point in pixels. If a gesture is in progress, the focal point is between each of the pointers forming the gesture.

If isInProgress would return false, the result of this function is undefined.

focusY

Added in 1.4.0-alpha05
val focusYFloat

The Y coordinate of the current gesture's focal point in pixels. If a gesture is in progress, the focal point is between each of the pointers forming the gesture.

If isInProgress would return false, the result of this function is undefined.

isInProgress

Added in 1.4.0-alpha05
val isInProgressBoolean

Whether a zoom gesture is in progress.

isQuickZoomEnabled

Added in 1.4.0-alpha05
var isQuickZoomEnabledBoolean

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.4.0-alpha05
var isStylusZoomEnabledBoolean

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.

scaleFactor

Added in 1.4.0-alpha05
val scaleFactorFloat

timeDelta

Added in 1.4.0-alpha05
val timeDeltaLong