Gestures

This documentation was generated with the assistance of AI. Please report any inaccuracies.

GestureBitmapView combines three detectors: Android’s GestureDetector (scroll, fling, single tap, double tap and long press), Android’s ScaleGestureDetector (pinch) and the library’s own RotationGestureDetector (two-finger rotation). On every touch event the view feeds each enabled detector, redraws, and when the last finger is lifted animates the bitmap back within its scale and scroll limits.

flowchart TD T["Touch event"] --> DOWN{"First finger down?"} DOWN -->|yes| C["Cancel running animations"] DOWN -->|no| G C --> G["GestureDetector: scroll, fling, taps
(when scrollEnabled, twoFingerScrollEnabled or scaleEnabled)"] G --> S["ScaleGestureDetector: pinch
(when scaleEnabled)"] S --> R["RotationGestureDetector: rotation
(when rotationEnabled)"] R --> UP{"Last finger up?"} UP -->|yes| L["Animate back within scale and scroll limits"] UP -->|no| E["Redraw"] L --> E

Scroll (translation)

Dragging moves the bitmap. Three flags control which fingers can scroll:

scrollEnabled twoFingerScrollEnabled exclusiveTwoFingerScrollEnabled Result

false

any

any

No scrolling.

true

any

false (default)

Scroll with one or two fingers; the number of fingers is not checked (two-finger scroll can be combined with pinch and rotation).

true

true (default)

true

Scroll with two fingers only; one-finger drags are ignored.

true

false

true

Scroll with one finger only; drags with more than one finger are ignored.

Defaults: scrollEnabled = true, twoFingerScrollEnabled = true, exclusiveTwoFingerScrollEnabled = false.

Scroll limits and bounce

The bitmap can be dragged past its edges by up to leftScrollMargin, topScrollMargin, rightScrollMargin and bottomScrollMargin pixels (100 by default). When an edge of the bitmap reaches its limit the matching OnTopBoundReachedListener, OnBottomBoundReachedListener, OnLeftBoundReachedListener or OnRightBoundReachedListener is notified. If the bitmap is smaller than the view along an axis, scrolling along that axis keeps it near the center. When the finger is lifted, the bitmap animates back so that it no longer exceeds its limits, which produces a bounce effect. Set the margins to 0 to avoid any overshoot.

With scrollTensionEnabled = true (default) the overshoot is progressive, like a stretched rubber band. The margins are the maximum overshoot: while the finger drags the bitmap past a limit, each outward finger movement is multiplied by (1 - overscroll / margin)^scrollTension (the base kept between 0 and 1), where overscroll is how far the bitmap already is past the limit. scrollTension (default 1, linear) adjusts how strong the resistance is: larger values make it grow faster and feel stiffer, values between 0 and 1 make it softer, and 0 removes it. Movement therefore slows down gradually and asymptotically approaches the margin instead of stopping against a hard wall. Movement towards the limits, and movement within the limits, always follows the finger 1:1. With scrollTensionEnabled = false, scrollTension = 0 or a margin of 0, the bitmap follows the finger 1:1 and is hard clamped at the margin. Tension only applies while dragging: fling, smoothScrollBy and the bounce back animation that runs when the finger is lifted are not affected, and the bound reached listeners keep their behavior.

Limits are computed from the area where the bitmap is currently displayed, so they follow scale and rotation changes made in the middle of a gesture. If a pinch or a rotation leaves the bitmap beyond its scroll limits, dragging never moves it more than the finger does: it can only be dragged back towards the limits, and the remaining excess is corrected by the animation that runs when the fingers are lifted (or the gesture is cancelled). No animation is started when the bitmap is already within its limits, or when no gesture changed the transformation during the touch (a plain tap keeps a transformation set programmatically, even beyond the limits). Nothing is scrolled while no bitmap is set.

Fling

A fling (fast swipe) follows the same scrollEnabled/two-finger rules as scroll. It produces a decelerating animated scroll that starts at the velocity of the finger: its distance is half the fling velocity multiplied by animationDurationMillis (converted to seconds), still bounded by the scroll limits. OnScrollAnimationCompletedListener is notified when the animation ends.

Zoom (scale)

Pinch

With scaleEnabled = true (default), pinching scales the bitmap around the focal point between the fingers. During the gesture the scale is clamped to the range [minScale - minScaleMargin, maxScale + maxScaleMargin], so the bitmap can be zoomed slightly beyond its limits. When the fingers are lifted, a scale outside [minScale, maxScale] animates back to the nearest limit. At the end of that animation:

  • OnMinScaleReachedListener is notified if the scale is at or below minScale, and OnMaxScaleReachedListener if it is at or above maxScale;

  • OnScaleAnimationCompletedListener is notified;

  • the current rotation is kept: only the scale is animated back to minScale, and afterwards the translation is brought back within the scroll limits. OnRotateAndTranslateAnimationCompletedListener is not notified in this case.

When a double tap returns to the minimum scale (see Double tap), rotation and translation are animated back to zero within the same animation, so the bitmap returns to its initial position in a single movement, and OnRotateAndTranslateAnimationCompletedListener is also notified.

minScale (default 1.0) and maxScale (default 10.0) are relative to the initial display defined by displayType: a scale of 1.0 means the bitmap is shown as initially fitted.

Double tap

With doubleTapEnabled = true (default), a double tap zooms in with an animation centered on the tapped point. Each double tap adds scaleFactorJump (default 3.0) to the current scale, up to maxScale. Once maxScale is reached, the next double tap zooms back to a scale of 1.0 (kept within [minScale, maxScale]), resetting rotation and translation when it reaches the minimum scale, and the cycle starts again. A pinch gesture restarts the cycle in the zoom-in direction.

OnDoubleTapListener is notified on every double tap, even when doubleTapEnabled is false, so you can implement your own double-tap behavior.

Taps are detected by the same GestureDetector as scrolling, which only receives events while at least one of scrollEnabled, twoFingerScrollEnabled or scaleEnabled is true.

Rotation

With rotationEnabled = true (default), rotating two fingers rotates the bitmap around the midpoint between them. There is no rotation limit, and the rotation is kept after the fingers are lifted (it is reset only when a double tap returns to the minimum scale, or by resetTransformationParameters()). Disable it with rotationEnabled = false or app:rotationEnabled="false".

Rotation fling

With rotationFlingEnabled = true (default, only applies when rotationEnabled is true), when the fingers are lifted while the bitmap is still rotating, the rotation does not stop suddenly. It continues at the angular velocity of the fingers (at least 0.5 rad/s is required) and decelerates until it stops, over animationDurationMillis, around the last pivot (the midpoint between the fingers). The fling starts when the last finger is lifted, as long as it is lifted within 150 ms of the rotation ending (the first finger being lifted); otherwise no fling is made. This keeps small movements of the remaining finger from cancelling it, and the rotation fling replaces any scroll fling made by that same release. Any new touch cancels it. When it finishes, the usual limit correction runs; if the scale is outside its limits, it is animated back to the nearest limit keeping the rotation reached by the fling, and the translation is brought back within the scroll limits. Disable it with rotationFlingEnabled = false or app:rotationFlingEnabled="false".

Using RotationGestureDetector on its own

RotationGestureDetector can be reused in your own views. Feed it touch events and implement OnRotationGestureListener:

private val rotationDetector = RotationGestureDetector(
    object : RotationGestureDetector.OnRotationGestureListener {
        override fun onRotation(
            gestureDetector: RotationGestureDetector,
            angle: Float,
            pivotX: Float,
            pivotY: Float
        ): Boolean {
            // angle: variation in radians since the previous event
            // gestureDetector.deltaAngleStart: variation since both fingers touched down
            rotateContentBy(angle, pivotX, pivotY)
            return true
        }
    })

override fun onTouchEvent(event: MotionEvent?): Boolean {
    rotationDetector.onTouchEvent(event)
    return true
}

onRotation is called on each move event while two fingers are down, with the angle variation (radians) relative to the previous event and the pivot at the midpoint between the fingers. onTouchEvent returns the value returned by the listener for those events and false otherwise. The detector also exposes the finger coordinates (initialX1, initialY1, initialX2, initialY2, currentX1, currentY1, currentX2, currentY2), the angles (initialAngle, currentAngle, deltaAngleStart, deltaAngle) and the pivot (pivotX, pivotY). The listener property can be replaced at any time.

When the rotation ends (a tracked finger is lifted or the gesture is cancelled), onRotationEnd(gestureDetector, angularVelocity, pivotX, pivotY) is called once, with the smoothed angular velocity in radians per second. It reports 0 when the gesture was cancelled or the fingers were lifted more than 100 ms after the last move. The method has an empty default body, and the last velocity is also available through the angularVelocity property.

Only two fingers are tracked. Additional fingers are ignored, and when one of the tracked fingers is lifted while others remain on the screen, tracking restarts with the remaining ones, so that no sudden rotation is reported.

Interaction with parent views

When a second finger touches the view, it asks its parent not to intercept touch events (requestDisallowInterceptTouchEvent(true)), so that scrolling containers do not steal pinch and rotation gestures.

Single tap and long press

A confirmed single tap calls performClick() and a long press calls performLongClick() (only when the view is long-clickable and no pinch is in progress), so the standard setOnClickListener and setOnLongClickListener work with the view.

Animations

Double-tap zoom, the return within limits, flings and the double tap reset to the minimum scale are animated over animationDurationMillis (default 200 ms) with a decelerating movement. Any running animation is cancelled when a new touch starts, when the bitmap, displayType or view size changes, or when the transformation is set programmatically.