Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pan shift #116

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
app:hasClickableChildren="true"
app:horizontalPanEnabled="true"
app:verticalPanEnabled="true"
app:panVerticalPadding="200"
app:panHorizontalPadding="100"
app:overpanFactor=".3"
app:zoomEnabled="true">

<com.otaliastudios.zoom.demo.ColorGridView
Expand Down
9 changes: 9 additions & 0 deletions library/src/main/java/com/otaliastudios/zoom/ZoomApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ interface ZoomApi {
*/
fun cancelAnimations(): Boolean

fun setPanVerticalPadding(padding: Int)

fun setPanHorizontalPadding(padding: Int)

fun setOverpanFactor(factor: Float)

companion object {

/**
Expand Down Expand Up @@ -489,5 +495,8 @@ interface ZoomApi {
* The default value for [setAlignment].
*/
const val ALIGNMENT_DEFAULT = com.otaliastudios.zoom.Alignment.CENTER

const val PAN_VERTICAL_PADDING_DEFAULT = 0
const val PAN_HORIZONTAL_PADDING_DEFAULT = 0
}
}
22 changes: 19 additions & 3 deletions library/src/main/java/com/otaliastudios/zoom/ZoomEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Matrix
import android.graphics.RectF
import android.view.*
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.view.ViewTreeObserver
import com.otaliastudios.zoom.ZoomApi.*
import com.otaliastudios.zoom.internal.UpdatesDispatcher
import com.otaliastudios.zoom.internal.matrix.MatrixController
import com.otaliastudios.zoom.internal.StateController
import com.otaliastudios.zoom.internal.UpdatesDispatcher
import com.otaliastudios.zoom.internal.gestures.PinchDetector
import com.otaliastudios.zoom.internal.gestures.ScrollFlingDetector
import com.otaliastudios.zoom.internal.matrix.MatrixController
import com.otaliastudios.zoom.internal.matrix.MatrixUpdate
import com.otaliastudios.zoom.internal.movement.PanManager
import com.otaliastudios.zoom.internal.movement.ZoomManager
Expand Down Expand Up @@ -568,6 +571,19 @@ internal constructor(context: Context) : ZoomApi {
matrixController.setContainerSize(width, height, applyTransformation)
}


override fun setPanVerticalPadding(padding: Int) {
panManager.panVerticalPadding = padding
}

override fun setPanHorizontalPadding(padding: Int) {
panManager.panHorizontalPadding = padding
}

override fun setOverpanFactor(factor: Float) {
panManager.overpanFactor = factor
}

/**
* Clears the current state, and stops dispatching matrix events
* until the view is laid out again and [ZoomEngine.setContentSize]
Expand Down
7 changes: 7 additions & 0 deletions library/src/main/java/com/otaliastudios/zoom/ZoomImageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.view.MotionEvent
import android.widget.ImageView
import androidx.annotation.AttrRes
import com.otaliastudios.zoom.ZoomApi.ZoomType
import com.otaliastudios.zoom.internal.movement.PanManager


/**
Expand Down Expand Up @@ -55,6 +56,9 @@ open class ZoomImageView private constructor(
val transformationGravity = a.getInt(R.styleable.ZoomEngine_transformationGravity, ZoomApi.TRANSFORMATION_GRAVITY_AUTO)
val alignment = a.getInt(R.styleable.ZoomEngine_alignment, ZoomApi.ALIGNMENT_DEFAULT)
val animationDuration = a.getInt(R.styleable.ZoomEngine_animationDuration, ZoomEngine.DEFAULT_ANIMATION_DURATION.toInt()).toLong()
val horizontalPadding = a.getInt(R.styleable.ZoomEngine_panHorizontalPadding, ZoomApi.PAN_HORIZONTAL_PADDING_DEFAULT)
val verticalPadding = a.getInt(R.styleable.ZoomEngine_panVerticalPadding, ZoomApi.PAN_VERTICAL_PADDING_DEFAULT)
val overpanFactor = a.getFloat(R.styleable.ZoomEngine_overpanFactor, PanManager.DEFAULT_OVERPAN_FACTOR)
a.recycle()

engine.setContainer(this)
Expand Down Expand Up @@ -84,6 +88,9 @@ open class ZoomImageView private constructor(
setAnimationDuration(animationDuration)
setMinZoom(minZoom, minZoomMode)
setMaxZoom(maxZoom, maxZoomMode)
setPanHorizontalPadding(horizontalPadding)
setPanVerticalPadding(verticalPadding)
setOverpanFactor(overpanFactor)

imageMatrix = mMatrix
scaleType = ScaleType.MATRIX
Expand Down
12 changes: 11 additions & 1 deletion library/src/main/java/com/otaliastudios/zoom/ZoomLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import android.content.Context
import android.graphics.Canvas
import android.graphics.Matrix
import android.util.AttributeSet
import android.view.*
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.FrameLayout
import androidx.annotation.AttrRes
import com.otaliastudios.zoom.ZoomApi.ZoomType
import com.otaliastudios.zoom.internal.movement.PanManager


/**
Expand Down Expand Up @@ -63,6 +67,9 @@ open class ZoomLayout private constructor(
val transformationGravity = a.getInt(R.styleable.ZoomEngine_transformationGravity, ZoomApi.TRANSFORMATION_GRAVITY_AUTO)
val alignment = a.getInt(R.styleable.ZoomEngine_alignment, ZoomApi.ALIGNMENT_DEFAULT)
val animationDuration = a.getInt(R.styleable.ZoomEngine_animationDuration, ZoomEngine.DEFAULT_ANIMATION_DURATION.toInt()).toLong()
val horizontalPadding = a.getInt(R.styleable.ZoomEngine_panHorizontalPadding, ZoomApi.PAN_HORIZONTAL_PADDING_DEFAULT)
val verticalPadding = a.getInt(R.styleable.ZoomEngine_panVerticalPadding, ZoomApi.PAN_VERTICAL_PADDING_DEFAULT)
val overpanFactor = a.getFloat(R.styleable.ZoomEngine_overpanFactor, PanManager.DEFAULT_OVERPAN_FACTOR)
a.recycle()

engine.setContainer(this)
Expand All @@ -88,6 +95,9 @@ open class ZoomLayout private constructor(
setMinZoom(minZoom, minZoomMode)
setMaxZoom(maxZoom, maxZoomMode)
setHasClickableChildren(hasChildren)
setPanHorizontalPadding(horizontalPadding)
setPanVerticalPadding(verticalPadding)
setOverpanFactor(overpanFactor)

setWillNotDraw(false)
}
Expand Down
17 changes: 14 additions & 3 deletions library/src/main/java/com/otaliastudios/zoom/ZoomSurfaceView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package com.otaliastudios.zoom

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.graphics.Color
import android.graphics.Matrix
import android.graphics.RectF
import android.graphics.SurfaceTexture
import android.opengl.GLSurfaceView
import android.os.Build
import android.util.AttributeSet
import android.view.*
import android.view.MotionEvent
import android.view.Surface
import androidx.annotation.RequiresApi
import androidx.annotation.UiThread
import androidx.annotation.WorkerThread
import com.otaliastudios.zoom.ZoomApi.ZoomType
import com.otaliastudios.opengl.core.EglConfigChooser
import com.otaliastudios.opengl.core.EglContextFactory
import com.otaliastudios.opengl.draw.GlRect
Expand All @@ -19,6 +22,8 @@ import com.otaliastudios.opengl.extensions.scale
import com.otaliastudios.opengl.extensions.translate
import com.otaliastudios.opengl.program.GlFlatProgram
import com.otaliastudios.opengl.program.GlTextureProgram
import com.otaliastudios.zoom.ZoomApi.ZoomType
import com.otaliastudios.zoom.internal.movement.PanManager
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

Expand Down Expand Up @@ -175,6 +180,9 @@ open class ZoomSurfaceView private constructor(
val transformationGravity = a.getInt(R.styleable.ZoomEngine_transformationGravity, ZoomApi.TRANSFORMATION_GRAVITY_AUTO)
val alignment = a.getInt(R.styleable.ZoomEngine_alignment, ZoomApi.ALIGNMENT_DEFAULT)
val animationDuration = a.getInt(R.styleable.ZoomEngine_animationDuration, ZoomEngine.DEFAULT_ANIMATION_DURATION.toInt()).toLong()
val horizontalPadding = a.getInt(R.styleable.ZoomEngine_panHorizontalPadding, ZoomApi.PAN_HORIZONTAL_PADDING_DEFAULT)
val verticalPadding = a.getInt(R.styleable.ZoomEngine_panVerticalPadding, ZoomApi.PAN_VERTICAL_PADDING_DEFAULT)
val overpanFactor = a.getFloat(R.styleable.ZoomEngine_overpanFactor, PanManager.DEFAULT_OVERPAN_FACTOR)
a.recycle()

engine.setContainer(this)
Expand Down Expand Up @@ -202,6 +210,9 @@ open class ZoomSurfaceView private constructor(
setAnimationDuration(animationDuration)
setMinZoom(minZoom, minZoomMode)
setMaxZoom(maxZoom, maxZoomMode)
setPanHorizontalPadding(horizontalPadding)
setPanVerticalPadding(verticalPadding)
setOverpanFactor(overpanFactor)

setEGLContextFactory(EglContextFactory.GLES2)
setEGLConfigChooser(EglConfigChooser.GLES2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.otaliastudios.zoom.internal.movement

import android.annotation.SuppressLint
import android.view.Gravity
import com.otaliastudios.zoom.*
import com.otaliastudios.zoom.Alignment
import com.otaliastudios.zoom.ScaledPoint
import com.otaliastudios.zoom.ZoomApi
import com.otaliastudios.zoom.internal.matrix.MatrixController
import kotlin.math.min

Expand All @@ -23,6 +25,9 @@ internal class PanManager(provider: () -> MatrixController) : MovementManager(pr
internal var horizontalPanEnabled = true
internal var verticalPanEnabled = true
internal var alignment = ZoomApi.ALIGNMENT_DEFAULT
internal var panVerticalPadding = 0
internal var panHorizontalPadding = 0
internal var overpanFactor = DEFAULT_OVERPAN_FACTOR

/** whether overpan is enabled, horizontally or vertically */
override val isOverEnabled get() = horizontalOverPanEnabled || verticalOverPanEnabled
Expand Down Expand Up @@ -58,7 +63,9 @@ internal class PanManager(provider: () -> MatrixController) : MovementManager(pr
val fix = checkBounds(horizontal, false).toInt()
val alignment = if (horizontal) Alignment.getHorizontal(alignment) else Alignment.getVertical(alignment)
@Suppress("CascadeIf")
if (contentDim > containerDim) {
if (contentDim > containerDim &&
panHorizontalPadding == ZoomApi.PAN_HORIZONTAL_PADDING_DEFAULT &&
panVerticalPadding == ZoomApi.PAN_VERTICAL_PADDING_DEFAULT) {
// Content is bigger. We can move between 0 and extraSpace, but since our pans
// are negative, we must invert the sign.
val extraSpace = contentDim - containerDim
Expand Down Expand Up @@ -139,7 +146,8 @@ internal class PanManager(provider: () -> MatrixController) : MovementManager(pr
}
min -= overScroll
max += overScroll
val desired = value.coerceIn(min, max)
val padding = if (horizontal) panHorizontalPadding else panVerticalPadding
val desired = value.coerceIn(min - padding, max + padding)
return desired - value
}

Expand All @@ -150,8 +158,8 @@ internal class PanManager(provider: () -> MatrixController) : MovementManager(pr
@ZoomApi.ScaledPan
internal val maxOverPan: Float
get() {
val overX = controller.containerWidth * DEFAULT_OVERPAN_FACTOR
val overY = controller.containerHeight * DEFAULT_OVERPAN_FACTOR
val overX = controller.containerWidth * overpanFactor
val overY = controller.containerHeight * overpanFactor
return min(overX, overY)
}

Expand Down Expand Up @@ -184,6 +192,6 @@ internal class PanManager(provider: () -> MatrixController) : MovementManager(pr
/**
* The default overscrolling factor
*/
private const val DEFAULT_OVERPAN_FACTOR = 0.10f
internal const val DEFAULT_OVERPAN_FACTOR = 0.10f
}
}
3 changes: 3 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
<flag name="none" value="0x44" />
</attr>
<attr name="animationDuration" format="integer" />
<attr name="panVerticalPadding" format="integer" />
<attr name="panHorizontalPadding" format="integer" />
<attr name="overpanFactor" format="float"/>
</declare-styleable>

</resources>