-
- All Implemented Interfaces:
-
com.otaliastudios.zoom.ZoomApi
public class ZoomEngine implements 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:
Pass the container view in the constructor
Notify the helper of the content size, using setContentSize
Pass touch events to onInterceptTouchEvent and onTouchEvent
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public interfaceZoomEngine.ListenerAn interface to listen for updates in the inner matrix. This will be called typically on animation frames.
public abstract classZoomEngine.SimpleListenerA simple implementation of Listener that will extract the translation and scale values from the output matrix.
public classZoomEngine.Companion
-
Field Summary
Fields Modifier and Type Field Description private final FloatrealZoomprivate final AbsolutePointpanprivate final FloatpanXprivate final FloatpanYprivate final ScaledPointscaledPanprivate final FloatscaledPanXprivate final FloatscaledPanYprivate final Matrixmatrixprivate final FloatcontentWidthprivate final FloatcontentHeightprivate final FloatcontainerWidthprivate final FloatcontainerHeightprivate final Floatzoom
-
Constructor Summary
Constructors Constructor Description ZoomEngine(Context context, View container)Constructs an helper instance. ZoomEngine(Context context, View container, ZoomEngine.Listener listener)Constructs an helper instance. ZoomEngine(Context context)
-
Method Summary
Modifier and Type Method Description FloatgetRealZoom()AbsolutePointgetPan()FloatgetPanX()FloatgetPanY()ScaledPointgetScaledPan()FloatgetScaledPanX()FloatgetScaledPanY()final MatrixgetMatrix()final FloatgetContentWidth()final FloatgetContentHeight()final FloatgetContainerWidth()final FloatgetContainerHeight()FloatgetZoom()final UnitaddListener(ZoomEngine.Listener listener)Registers a new Listener to be notified of matrix updates. final UnitremoveListener(ZoomEngine.Listener listener)Removes a previously registered listener. UnitsetOverScrollHorizontal(Boolean overScroll)Controls whether the content should be over-scrollable horizontally. UnitsetOverScrollVertical(Boolean overScroll)Controls whether the content should be over-scrollable vertically. UnitsetOverPanRange(OverPanRangeProvider provider)Set the OverPanRangeProvider that specifies the amount of overpan to allow. UnitsetHorizontalPanEnabled(Boolean enabled)Controls whether horizontal panning using gestures is enabled. UnitsetVerticalPanEnabled(Boolean enabled)Controls whether vertical panning using gestures is enabled. UnitsetOverPinchable(Boolean overPinchable)Controls whether the content should be overPinchable. UnitsetOverZoomRange(OverZoomRangeProvider provider)Set the OverZoomRangeProvider that specifies the amount of overzoom to allow. UnitsetZoomEnabled(Boolean enabled)Controls whether zoom using pinch gesture is enabled or not. UnitsetFlingEnabled(Boolean enabled)Controls whether fling gesture is enabled or not. UnitsetScrollEnabled(Boolean enabled)Controls whether scroll gesture is enabled or not. UnitsetOneFingerScrollEnabled(Boolean enabled)Controls whether one finger scroll gesture is enabled or not. UnitsetTwoFingersScrollEnabled(Boolean enabled)Controls whether two fingers scroll gesture is enabled or not. UnitsetThreeFingersScrollEnabled(Boolean enabled)Controls whether three fingers scroll gesture is enabled or not. UnitsetAllowFlingInOverscroll(Boolean allow)Controls whether fling events are allowed when the view is in an overscrolled state. UnitsetTransformation(Integer transformation, Integer gravity)Sets the base transformation to be applied to the content. UnitsetAlignment(Integer alignment)Sets the content alignment. final UnitsetContainer(View container)Set a container to perform transformations on. final UnitsetContentSize(RectF rect)Notifies the helper of the content size (be it a child View, a Bitmap, or whatever else). final UnitsetContentSize(Float width, Float height, Boolean applyTransformation)Notifies the helper of the content size (be it a child View, a Bitmap, or whatever else). final UnitsetContentSize(Float width, Float height)Notifies the helper of the content size (be it a child View, a Bitmap, or whatever else). final UnitsetContainerSize(Float width, Float height, Boolean applyTransformation)Sets the size of the container view. final UnitsetContainerSize(Float width, Float height)Sets the size of the container view. final Unitclear()Clears the current state, and stops dispatching matrix events until the view is laid out again and ZoomEngine.setContentSize is called. final BooleanonInterceptTouchEvent(MotionEvent ev)This is required when the content is a View that has clickable hierarchies inside. final BooleanonTouchEvent(MotionEvent ev)Process the given touch event. UnitmoveTo(Float zoom, Float x, Float y, Boolean animate)A low level API that can animate both zoom and pan at the same time. UnitmoveToCenter(Float zoom, Boolean animate)Moves to the center of the content. UnitpanTo(Float x, Float y, Boolean animate)Pans the content until the top-left coordinates match the given x-y values. UnitpanBy(Float dx, Float dy, Boolean animate)Pans the content by the given quantity in dx-dy values. UnitzoomTo(Float zoom, Boolean animate)Zooms to the given scale. UnitzoomBy(Float zoomFactor, Boolean animate)Applies the given factor to the current zoom. UnitzoomIn()Applies a small, animated zoom-in. UnitzoomOut()Applies a small, animated zoom-out. UnitrealZoomTo(Float realZoom, Boolean animate)Animates the actual matrix zoom to the given value. FloatgetMaxZoom()Get the currently allowed max zoom. IntegergetMaxZoomType()Get the currently set max zoom type. UnitsetMaxZoom(Float maxZoom, Integer type)Which is the max zoom that should be allowed. FloatgetMinZoom()Get the currently allowed min zoom. IntegergetMinZoomType()Get the currently set min zoom type. UnitsetMinZoom(Float minZoom, Integer type)Which is the min zoom that should be allowed. UnitsetAnimationDuration(Long duration)Sets the duration of animations triggered by zoom and pan APIs. BooleancancelAnimations()Cancels all currently active animations triggered by either API calls with animate = trueor touch input flings.final IntegercomputeHorizontalScrollOffset()Helper for implementing View.computeHorizontalScrollOffset in custom views. final IntegercomputeHorizontalScrollRange()Helper for implementing View.computeHorizontalScrollRange in custom views. final IntegercomputeVerticalScrollOffset()Helper for implementing View.computeVerticalScrollOffset in custom views. final IntegercomputeVerticalScrollRange()Helper for implementing View.computeVerticalScrollRange in custom views. -
-
Constructor Detail
-
ZoomEngine
ZoomEngine(Context context, View container)
Constructs an helper instance.- Parameters:
context- a valid contextcontainer- the view hosting the zoomable content
-
ZoomEngine
ZoomEngine(Context context, View container, ZoomEngine.Listener listener)
Constructs an helper instance.- Parameters:
context- a valid contextcontainer- the view hosting the zoomable contentlistener- a listener for events
-
ZoomEngine
ZoomEngine(Context context)
-
-
Method Detail
-
getRealZoom
Float getRealZoom()
-
getPan
AbsolutePoint getPan()
-
getScaledPan
ScaledPoint getScaledPan()
-
getScaledPanX
Float getScaledPanX()
-
getScaledPanY
Float getScaledPanY()
-
getContentWidth
final Float getContentWidth()
-
getContentHeight
final Float getContentHeight()
-
getContainerWidth
final Float getContainerWidth()
-
getContainerHeight
final Float getContainerHeight()
-
addListener
final Unit addListener(ZoomEngine.Listener listener)
Registers a new Listener to be notified of matrix updates.
- Parameters:
listener- the new listener
-
removeListener
final Unit removeListener(ZoomEngine.Listener listener)
Removes a previously registered listener.
- Parameters:
listener- the listener to be removed
-
setOverScrollHorizontal
Unit setOverScrollHorizontal(Boolean overScroll)
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.
- Parameters:
overScroll- whether to allow horizontal over scrolling
-
setOverScrollVertical
Unit setOverScrollVertical(Boolean overScroll)
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.
- Parameters:
overScroll- whether to allow vertical over scrolling
-
setOverPanRange
Unit setOverPanRange(OverPanRangeProvider provider)
Set the OverPanRangeProvider that specifies the amount of overpan to allow.
-
setHorizontalPanEnabled
Unit setHorizontalPanEnabled(Boolean enabled)
Controls whether horizontal panning using gestures is enabled.
- Parameters:
enabled- true enables horizontal panning, false disables it
-
setVerticalPanEnabled
Unit setVerticalPanEnabled(Boolean enabled)
Controls whether vertical panning using gestures is enabled.
- Parameters:
enabled- true enables vertical panning, false disables it
-
setOverPinchable
Unit setOverPinchable(Boolean overPinchable)
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.
- Parameters:
overPinchable- whether to allow over pinching
-
setOverZoomRange
Unit setOverZoomRange(OverZoomRangeProvider provider)
Set the OverZoomRangeProvider that specifies the amount of overzoom to allow.
- Parameters:
provider- the range provider
-
setZoomEnabled
Unit setZoomEnabled(Boolean enabled)
Controls whether zoom using pinch gesture is enabled or not.
- Parameters:
enabled- true enables zooming, false disables it
-
setFlingEnabled
Unit setFlingEnabled(Boolean enabled)
Controls whether fling gesture is enabled or not.
- Parameters:
enabled- true enables fling gesture, false disables it
-
setScrollEnabled
Unit setScrollEnabled(Boolean enabled)
Controls whether scroll gesture is enabled or not.
- Parameters:
enabled- true enables scroll gesture, false disables it
-
setOneFingerScrollEnabled
Unit setOneFingerScrollEnabled(Boolean enabled)
Controls whether one finger scroll gesture is enabled or not.
- Parameters:
enabled- true enables one finger scroll gesture, false disables it
-
setTwoFingersScrollEnabled
Unit setTwoFingersScrollEnabled(Boolean enabled)
Controls whether two fingers scroll gesture is enabled or not.
- Parameters:
enabled- true enables two fingers scroll gesture, false disables it
-
setThreeFingersScrollEnabled
Unit setThreeFingersScrollEnabled(Boolean enabled)
Controls whether three fingers scroll gesture is enabled or not.
- Parameters:
enabled- true enables three fingers scroll gesture, false disables it
-
setAllowFlingInOverscroll
Unit setAllowFlingInOverscroll(Boolean allow)
Controls whether fling events are allowed when the view is in an overscrolled state.
- Parameters:
allow- true allows fling in overscroll, false disables it
-
setTransformation
Unit setTransformation(Integer transformation, Integer gravity)
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.
- Parameters:
transformation- the transformation typegravity- the transformation gravity.
-
setAlignment
Unit setAlignment(Integer alignment)
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.
Keep in mind that this is disabled when the content is larger than the container, because a forced alignment in this case would result in part of the content being unreachable.
- Parameters:
alignment- the new alignment
-
setContainer
final Unit setContainer(View container)
Set a container to perform transformations on. This method can only be called once at initialization time. It throws an exception if it is called twice.
- Parameters:
container- view
-
setContentSize
@Deprecated(message = Deprecated in favor of not using RectF class, replaceWith = @ReplaceWith(imports = {}, expression = setContentSize(width, height))) final Unit setContentSize(RectF rect)
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.
- Parameters:
rect- the content rect
-
setContentSize
@JvmOverloads() final Unit setContentSize(Float width, Float height, Boolean applyTransformation)
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.
- Parameters:
width- the content widthheight- the content heightapplyTransformation- whether to apply the transformation defined by setTransformation
-
setContentSize
@JvmOverloads() final Unit setContentSize(Float width, Float height)
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.
- Parameters:
width- the content widthheight- the content height
-
setContainerSize
@JvmOverloads() final Unit setContainerSize(Float width, Float height, Boolean applyTransformation)
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.
However, there are cases where you might want to update it, for example during View.onSizeChanged (called during shared element transitions).
- Parameters:
width- the container widthheight- the container heightapplyTransformation- whether to apply the transformation defined by setTransformation
-
setContainerSize
@JvmOverloads() final Unit setContainerSize(Float width, Float height)
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.
However, there are cases where you might want to update it, for example during View.onSizeChanged (called during shared element transitions).
- Parameters:
width- the container widthheight- the container height
-
clear
final Unit clear()
Clears the current state, and stops dispatching matrix events until the view is laid out again and ZoomEngine.setContentSize is called.
-
onInterceptTouchEvent
final Boolean onInterceptTouchEvent(MotionEvent ev)
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.
- Parameters:
ev- the motion event
-
onTouchEvent
final Boolean onTouchEvent(MotionEvent ev)
Process the given touch event. If true is returned, implementors should not pass the call to super.
- Parameters:
ev- the motion event
-
moveTo
Unit moveTo(Float zoom, Float x, Float y, Boolean animate)
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.
- Parameters:
zoom- the desired zoom valuex- the desired left coordinatey- the desired top coordinateanimate- whether to animate the transition
-
moveToCenter
Unit moveToCenter(Float zoom, Boolean animate)
Moves to the center of the content.
- Parameters:
zoom- the desired zoom valueanimate- whether to animate the transition
-
panTo
Unit panTo(Float x, Float y, Boolean animate)
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.
- Parameters:
x- the desired left coordinatey- the desired top coordinateanimate- whether to animate the transition
-
panBy
Unit panBy(Float dx, Float dy, Boolean animate)
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.
In other words, asking to pan by 1 pixel might result in a bigger pan, if the content was zoomed in.
- Parameters:
dx- the desired delta xdy- the desired delta yanimate- whether to animate the transition
-
zoomTo
Unit zoomTo(Float zoom, Boolean animate)
Zooms to the given scale. This might not be the actual matrix zoom, see ZoomApi.zoom and ZoomApi.realZoom.
- Parameters:
zoom- the new scale valueanimate- whether to animate the transition
-
zoomBy
Unit zoomBy(Float zoomFactor, Boolean animate)
Applies the given factor to the current zoom.
- Parameters:
zoomFactor- a multiplicative factoranimate- whether to animate the transition
-
realZoomTo
Unit realZoomTo(Float realZoom, Boolean animate)
Animates the actual matrix zoom to the given value.
- Parameters:
realZoom- the new real zoom valueanimate- whether to animate the transition
-
getMaxZoom
Float getMaxZoom()
Get the currently allowed max zoom. If ZoomApi.setOverPinchable is set to true, this can be over-pinched for a brief time.
-
getMaxZoomType
Integer getMaxZoomType()
Get the currently set max zoom type.
-
setMaxZoom
Unit setMaxZoom(Float maxZoom, Integer type)
Which is the max zoom that should be allowed. If setOverPinchable is set to true, this can be over-pinched for a brief time.
- Parameters:
maxZoom- the max zoomtype- the constraint mode
-
getMinZoom
Float getMinZoom()
Get the currently allowed min zoom. If ZoomApi.setOverPinchable is set to true, this can be over-pinched for a brief time.
-
getMinZoomType
Integer getMinZoomType()
Get the currently set min zoom type.
-
setMinZoom
Unit setMinZoom(Float minZoom, Integer type)
Which is the min zoom that should be allowed. If setOverPinchable is set to true, this can be over-pinched for a brief time.
- Parameters:
minZoom- the min zoomtype- the constraint mode
-
setAnimationDuration
Unit setAnimationDuration(Long duration)
Sets the duration of animations triggered by zoom and pan APIs. Defaults to ZoomEngine.DEFAULT_ANIMATION_DURATION.
- Parameters:
duration- new animation duration
-
cancelAnimations
Boolean cancelAnimations()
Cancels all currently active animations triggered by either API calls with
animate = trueor touch input flings. If no animation is currently active this is a no-op.
-
computeHorizontalScrollOffset
final Integer computeHorizontalScrollOffset()
Helper for implementing View.computeHorizontalScrollOffset in custom views.
-
computeHorizontalScrollRange
final Integer computeHorizontalScrollRange()
Helper for implementing View.computeHorizontalScrollRange in custom views.
-
computeVerticalScrollOffset
final Integer computeVerticalScrollOffset()
Helper for implementing View.computeVerticalScrollOffset in custom views.
-
computeVerticalScrollRange
final Integer computeVerticalScrollRange()
Helper for implementing View.computeVerticalScrollRange in custom views.
-
-
-
-