ZoomGestureDetector
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:
-
In the
OnZoomGestureListener.onZoomEvent, when receivingZoomEvent.Move, get theZoomEvent.Move.incrementalScaleFactorand use the value withCameraControl.setZoomRatioif the factor is in the range ofZoomState.getMinZoomRatioandZoomState.getMaxZoomRatio. Then create an instance of theZoomGestureDetectorwith theOnZoomGestureListener. -
In the
View.onTouchEvent, callonTouchEventand pass theMotionEvents to theZoomGestureDetector.
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 ZoomGestureDetector.OnZoomGestureListenerThe listener for receiving notifications when gestures occur. |
public abstract class ZoomGestureDetector.ZoomEventThe zoom event that contains extended info about event state. |
public final class ZoomGestureDetector.ZoomEvent.Begin extends ZoomGestureDetector.ZoomEventThe beginning of a zoom gesture. |
public final class ZoomGestureDetector.ZoomEvent.End extends ZoomGestureDetector.ZoomEventThe end of a zoom gesture. |
public final class ZoomGestureDetector.ZoomEvent.Move extends ZoomGestureDetector.ZoomEventThe moving events of a gesture in progress. |
Public constructors |
|---|
ZoomGestureDetector(Creates a ZoomGestureDetector for detecting zooming gesture. |
Public methods |
|
|---|---|
final long |
Returns the time difference in milliseconds between the previous accepted zooming event and the current zooming event. |
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 |
@UiThreadAccepts |
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. |
final void |
setStylusZoomEnabled(boolean value)Whether the stylus zoom gesture, in which the user uses a stylus and presses the button, should perform zooming. |
Public constructors
ZoomGestureDetector
public ZoomGestureDetector(
@NonNull Context context,
@Px int spanSlop,
@Px int minSpan,
@NonNull ZoomGestureDetector.OnZoomGestureListener listener
)
Creates a ZoomGestureDetector for detecting zooming gesture.
| Parameters | |
|---|---|
@NonNull Context context |
The application context. |
@Px int spanSlop |
The distance in pixels touches can wander before a gesture to be interpreted as zooming. |
@Px 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
getTimeDelta
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
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
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
@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 |
|
setQuickZoomEnabled
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
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.