library / com.otaliastudios.zoom / ZoomEngine

ZoomEngine

open class ZoomEngine : ZoomApi

A low level class that listens to touch events and posts zoom and pan updates. The most useful output is a Matrix that can be used to do pretty much everything, from canvas drawing to View hierarchies translations.

Users are required to:

Types

Listener

interface Listener

An interface to listen for updates in the inner matrix. This will be called typically on animation frames.

SimpleListener

abstract class SimpleListener : Listener

A simple implementation of Listener that will extract the translation and scale values from the output matrix.

Constructors

<init>

ZoomEngine(context: Context, container: View)

Constructs an helper instance.

ZoomEngine(context: Context, container: View, listener: Listener)

Constructs an helper instance. Deprecated: use addListener to add a listener.

Properties

containerHeight

val containerHeight: Float

Returns the container height as passed to setContainerSize.

containerWidth

val containerWidth: Float

Returns the container width as passed to setContainerSize.

contentHeight

val contentHeight: Float

Returns the content height as passed to setContentSize.

contentWidth

val contentWidth: Float

Returns the content width as passed to setContentSize.

matrix

val matrix: Matrix

Returns the current matrix. This can be changed from the outside, but is not guaranteed to remain stable.

pan

open val pan: AbsolutePoint

The current pan as an AbsolutePoint.

panX

open val panX: Float

Returns the current horizontal pan value, in content absolute coordinates (that is, as if there was no zoom at all).

panY

open val panY: Float

Returns the current vertical pan value, in content absolute coordinates (that is, as if there was no zoom at all).

realZoom

open val realZoom: Float

Gets the current zoom value, including the base zoom that was eventually applied during the starting transformation, see setTransformation. This value will match the scaleX - scaleY values you get into the Matrix, and is the actual scale value of the content from its original size.

scaledPan

open val scaledPan: ScaledPoint

The current pan as a ScaledPoint.

scaledPanX

open val scaledPanX: Float

Returns the current horizontal pan value, in content scaled coordinates (that is, including the current zoom).

scaledPanY

open val scaledPanY: Float

Returns the current vertical pan value, in content scaled coordinates (that is, including the current zoom).

zoom

open val zoom: Float

Gets the current zoom value, which can be used as a reference when calling zoomTo or zoomBy.

Functions

addListener

fun addListener(listener: Listener): Unit

Registers a new Listener to be notified of matrix updates.

cancelAnimations

open fun cancelAnimations(): Boolean

Cancels all currently active animations triggered by either API calls with animate = true or touch input flings. If no animation is currently active this is a no-op.

clear

fun clear(): Unit

Clears the current state, and stops dispatching matrix events until the view is laid out again and ZoomEngine.setContentSize is called.

computeHorizontalScrollOffset

fun computeHorizontalScrollOffset(): Int

Helper for implementing View.computeHorizontalScrollOffset in custom views.

computeHorizontalScrollRange

fun computeHorizontalScrollRange(): Int

Helper for implementing View.computeHorizontalScrollRange in custom views.

computeVerticalScrollOffset

fun computeVerticalScrollOffset(): Int

Helper for implementing View.computeVerticalScrollOffset in custom views.

computeVerticalScrollRange

fun computeVerticalScrollRange(): Int

Helper for implementing View.computeVerticalScrollRange in custom views.

moveTo

open fun moveTo(zoom: Float, x: Float, y: Float, animate: Boolean): Unit

A low level API that can animate both zoom and pan at the same time. Zoom might not be the actual matrix scale, see ZoomApi.zoom and ZoomApi.realZoom. The coordinates are referred to the content size passed in setContentSize so they do not depend on current zoom.

onInterceptTouchEvent

fun onInterceptTouchEvent(ev: MotionEvent): Boolean

This is required when the content is a View that has clickable hierarchies inside. If true is returned, implementors should not pass the call to super.

onTouchEvent

fun onTouchEvent(ev: MotionEvent): Boolean

Process the given touch event. If true is returned, implementors should not pass the call to super.

panBy

open fun panBy(dx: Float, dy: Float, animate: Boolean): Unit

Pans the content by the given quantity in dx-dy values. These are referred to the content size passed in setContentSize, so they do not depend on current zoom.

panTo

open fun panTo(x: Float, y: Float, animate: Boolean): Unit

Pans the content until the top-left coordinates match the given x-y values. These are referred to the content size passed in setContentSize, so they do not depend on current zoom.

realZoomTo

open fun realZoomTo(realZoom: Float, animate: Boolean): Unit

Animates the actual matrix zoom to the given value.

removeListener

fun removeListener(listener: Listener): Unit

Removes a previously registered listener.

setAlignment

open fun setAlignment(alignment: Int): Unit

Sets the content alignment. Can be any of the constants defined in Alignment. The content will be aligned and forced to the specified side of the container. Defaults to ZoomApi.ALIGNMENT_DEFAULT.

setAllowFlingInOverscroll

open fun setAllowFlingInOverscroll(allow: Boolean): Unit

Controls whether fling events are allowed when the view is in an overscrolled state.

setAnimationDuration

open fun setAnimationDuration(duration: Long): Unit

Sets the duration of animations triggered by zoom and pan APIs. Defaults to ZoomEngine.DEFAULT_ANIMATION_DURATION.

setContainerSize

fun setContainerSize(width: Float, height: Float, applyTransformation: Boolean = false): Unit

Sets the size of the container view. Normally you don't need to call this because the size is detected from the container passed to the constructor using a global layout listener.

setContentSize

fun setContentSize(rect: RectF): Unit
fun setContentSize(width: Float, height: Float, applyTransformation: Boolean = false): Unit

Notifies the helper of the content size (be it a child View, a Bitmap, or whatever else). This is needed for the helper to start working.

setFlingEnabled

open fun setFlingEnabled(enabled: Boolean): Unit

Controls whether fling gesture is enabled or not.

setHorizontalPanEnabled

open fun setHorizontalPanEnabled(enabled: Boolean): Unit

Controls whether horizontal panning using gestures is enabled.

setMaxZoom

open fun setMaxZoom(maxZoom: Float, type: Int): Unit

Which is the max zoom that should be allowed. If setOverPinchable is set to true, this can be over-pinched for a brief time.

setMinZoom

open fun setMinZoom(minZoom: Float, type: Int): Unit

Which is the min zoom that should be allowed. If setOverPinchable is set to true, this can be over-pinched for a brief time.

setOneFingerScrollEnabled

open fun setOneFingerScrollEnabled(enabled: Boolean): Unit

Controls whether one finger scroll gesture is enabled or not.

setOverPinchable

open fun setOverPinchable(overPinchable: Boolean): Unit

Controls whether the content should be overPinchable. If it is, pinch events can change the zoom outside the safe bounds, than return to safe values.

setOverScrollHorizontal

open fun setOverScrollHorizontal(overScroll: Boolean): Unit

Controls whether the content should be over-scrollable horizontally. If it is, drag and fling horizontal events can scroll the content outside the safe area, then return to safe values.

setOverScrollVertical

open fun setOverScrollVertical(overScroll: Boolean): Unit

Controls whether the content should be over-scrollable vertically. If it is, drag and fling vertical events can scroll the content outside the safe area, then return to safe values.

setScrollEnabled

open fun setScrollEnabled(enabled: Boolean): Unit

Controls whether scroll gesture is enabled or not.

setThreeFingersScrollEnabled

open fun setThreeFingersScrollEnabled(enabled: Boolean): Unit

Controls whether three fingers scroll gesture is enabled or not.

setTransformation

open fun setTransformation(transformation: Int, gravity: Int): Unit

Sets the base transformation to be applied to the content. Defaults to ZoomApi.TRANSFORMATION_CENTER_INSIDE with Gravity.CENTER, which means that the content will be zoomed so that it fits completely inside the container.

setTwoFingersScrollEnabled

open fun setTwoFingersScrollEnabled(enabled: Boolean): Unit

Controls whether two fingers scroll gesture is enabled or not.

setVerticalPanEnabled

open fun setVerticalPanEnabled(enabled: Boolean): Unit

Controls whether vertical panning using gestures is enabled.

setZoomEnabled

open fun setZoomEnabled(enabled: Boolean): Unit

Controls whether zoom using pinch gesture is enabled or not.

zoomBy

open fun zoomBy(zoomFactor: Float, animate: Boolean): Unit

Applies the given factor to the current zoom.

zoomIn

open fun zoomIn(): Unit

Applies a small, animated zoom-in. Shorthand for zoomBy with factor 1.3.

zoomOut

open fun zoomOut(): Unit

Applies a small, animated zoom-out. Shorthand for zoomBy with factor 0.7.

zoomTo

open fun zoomTo(zoom: Float, animate: Boolean): Unit

Zooms to the given scale. This might not be the actual matrix zoom, see ZoomApi.zoom and ZoomApi.realZoom.

Inherited Functions

setTransformation

open fun setTransformation(transformation: Int): Unit

Sets the base transformation to be applied to the content. See setTransformation.

Companion Object Properties

DEFAULT_ANIMATION_DURATION

const val DEFAULT_ANIMATION_DURATION: Long

The default animation duration