ZoomGestureDetector


@RequiresApi(value = 21)
public final 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

static final int

The beginning of a zoom gesture.

static final int

The end of a zoom gesture.

static final int

The moving events of a gesture in progress.

Public constructors

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

Creates a ZoomGestureDetector for detecting zooming gesture.

Public methods

final long

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

final float

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

final float

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

final float

Returns the scaling factor from the previous zoom event to the current event.

final long

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

final boolean

Whether a zoom gesture is in progress.

final boolean

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

final boolean

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

final boolean

Accepts MotionEvents and dispatches events to a OnZoomGestureListener when appropriate.

final void
setQuickZoomEnabled(boolean isQuickZoomEnabled)

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

final void
setStylusZoomEnabled(boolean isStylusZoomEnabled)

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

Constants

ZOOM_GESTURE_BEGIN

public static final int ZOOM_GESTURE_BEGIN = 1

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

ZOOM_GESTURE_END

public static final int ZOOM_GESTURE_END = 2

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

ZOOM_GESTURE_MOVE

public static final int ZOOM_GESTURE_MOVE = 0

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

Public constructors

ZoomGestureDetector

Added in 1.4.0-alpha05
public ZoomGestureDetector(
    @NonNull Context context,
    int spanSlop,
    int minSpan,
    @NonNull ZoomGestureDetector.OnZoomGestureListener listener
)

Creates a ZoomGestureDetector for detecting zooming gesture.

Parameters
@NonNull Context context

The application context.

int spanSlop

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

int minSpan

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

@NonNull ZoomGestureDetector.OnZoomGestureListener listener

The listener to receive the callback.

Public methods

getEventTime

Added in 1.4.0-alpha05
public final long getEventTime()

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

getFocusX

Added in 1.4.0-alpha05
public final float getFocusX()

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.

getFocusY

Added in 1.4.0-alpha05
public final float getFocusY()

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.

getScaleFactor

Added in 1.4.0-alpha05
public final float getScaleFactor()

Returns the scaling factor from the previous zoom event to the current event. This value is defined as (currentSpan / previousSpan).

Returns
float

The current scaling factor.

getTimeDelta

Added in 1.4.0-alpha05
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.

isInProgress

Added in 1.4.0-alpha05
public final boolean isInProgress()

Whether a zoom gesture is in progress.

isQuickZoomEnabled

Added in 1.4.0-alpha05
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.4.0-alpha05
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.4.0-alpha05
@UiThread
public final boolean onTouchEvent(@NonNull MotionEvent event)

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
@NonNull MotionEvent event

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.

setQuickZoomEnabled

Added in 1.4.0-alpha05
public final void setQuickZoomEnabled(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.

setStylusZoomEnabled

Added in 1.4.0-alpha05
public final void setStylusZoomEnabled(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.