From fa1a47a4fecbda3143f72a8d812f7ed49828a1b9 Mon Sep 17 00:00:00 2001 From: Link184 Date: Tue, 24 Sep 2019 14:03:58 +0300 Subject: [PATCH 1/3] Added possibility to set pan shifts --- app/src/main/res/layout/activity_main.xml | 8 ++++--- .../java/com/otaliastudios/zoom/ZoomApi.kt | 9 ++++++++ .../java/com/otaliastudios/zoom/ZoomEngine.kt | 22 ++++++++++++++++--- .../java/com/otaliastudios/zoom/ZoomLayout.kt | 12 +++++++++- .../zoom/internal/movement/PanManager.kt | 21 +++++++++++++----- library/src/main/res/values/attrs.xml | 3 +++ 6 files changed, 62 insertions(+), 13 deletions(-) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 683d00d..f223cbd 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -8,13 +8,15 @@ MatrixController) : MovementManager(pr internal var horizontalPanEnabled = true internal var verticalPanEnabled = true internal var alignment = ZoomApi.ALIGNMENT_DEFAULT + internal var panVerticalShift = 0 + internal var panHorizontalShift = 0 + internal var overpanFactor = DEFAULT_OVERPAN_FACTOR /** whether overpan is enabled, horizontally or vertically */ override val isOverEnabled get() = horizontalOverPanEnabled || verticalOverPanEnabled @@ -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 && + panHorizontalShift == ZoomApi.PAN_HORIZONTAL_SHIFT_DEFAULT && + panVerticalShift == ZoomApi.PAN_VERTICAL_SHIFT_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 @@ -139,7 +146,9 @@ internal class PanManager(provider: () -> MatrixController) : MovementManager(pr } min -= overScroll max += overScroll - val desired = value.coerceIn(min, max) +// shift = abs(shift) + val shift = if (horizontal) panHorizontalShift else panVerticalShift + val desired = value.coerceIn(min - shift, max + shift) return desired - value } @@ -150,8 +159,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) } @@ -184,6 +193,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 } } \ No newline at end of file diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 30035b7..4feea37 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -54,6 +54,9 @@ + + + \ No newline at end of file From 54eacefc2453ff1bf2b0dcf3246bea55f2917e65 Mon Sep 17 00:00:00 2001 From: Link184 Date: Tue, 24 Sep 2019 14:29:56 +0300 Subject: [PATCH 2/3] Fixed view dimensions --- app/src/main/res/layout/activity_main.xml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f223cbd..a53dbf9 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -8,8 +8,9 @@ Date: Wed, 25 Sep 2019 10:13:23 +0300 Subject: [PATCH 3/3] Renamed shift to padding --- app/src/main/res/layout/activity_main.xml | 4 ++-- .../main/java/com/otaliastudios/zoom/ZoomApi.kt | 8 ++++---- .../java/com/otaliastudios/zoom/ZoomEngine.kt | 8 ++++---- .../com/otaliastudios/zoom/ZoomImageView.kt | 7 +++++++ .../java/com/otaliastudios/zoom/ZoomLayout.kt | 8 ++++---- .../com/otaliastudios/zoom/ZoomSurfaceView.kt | 17 ++++++++++++++--- .../zoom/internal/movement/PanManager.kt | 13 ++++++------- library/src/main/res/values/attrs.xml | 4 ++-- 8 files changed, 43 insertions(+), 26 deletions(-) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index a53dbf9..a487742 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -15,8 +15,8 @@ app:hasClickableChildren="true" app:horizontalPanEnabled="true" app:verticalPanEnabled="true" - app:panVerticalShift="200" - app:panHorizontalShift="100" + app:panVerticalPadding="200" + app:panHorizontalPadding="100" app:overpanFactor=".3" app:zoomEnabled="true"> diff --git a/library/src/main/java/com/otaliastudios/zoom/ZoomApi.kt b/library/src/main/java/com/otaliastudios/zoom/ZoomApi.kt index 8bf552f..5d1684c 100644 --- a/library/src/main/java/com/otaliastudios/zoom/ZoomApi.kt +++ b/library/src/main/java/com/otaliastudios/zoom/ZoomApi.kt @@ -411,9 +411,9 @@ interface ZoomApi { */ fun cancelAnimations(): Boolean - fun setPanVeritcalShift(shift: Int) + fun setPanVerticalPadding(padding: Int) - fun setPanHorizontalShift(shift: Int) + fun setPanHorizontalPadding(padding: Int) fun setOverpanFactor(factor: Float) @@ -496,7 +496,7 @@ interface ZoomApi { */ const val ALIGNMENT_DEFAULT = com.otaliastudios.zoom.Alignment.CENTER - const val PAN_VERTICAL_SHIFT_DEFAULT = 0 - const val PAN_HORIZONTAL_SHIFT_DEFAULT = 0 + const val PAN_VERTICAL_PADDING_DEFAULT = 0 + const val PAN_HORIZONTAL_PADDING_DEFAULT = 0 } } \ No newline at end of file diff --git a/library/src/main/java/com/otaliastudios/zoom/ZoomEngine.kt b/library/src/main/java/com/otaliastudios/zoom/ZoomEngine.kt index 1a77385..3b61b45 100644 --- a/library/src/main/java/com/otaliastudios/zoom/ZoomEngine.kt +++ b/library/src/main/java/com/otaliastudios/zoom/ZoomEngine.kt @@ -572,12 +572,12 @@ internal constructor(context: Context) : ZoomApi { } - override fun setPanVeritcalShift(shift: Int) { - panManager.panVerticalShift = shift + override fun setPanVerticalPadding(padding: Int) { + panManager.panVerticalPadding = padding } - override fun setPanHorizontalShift(shift: Int) { - panManager.panHorizontalShift = shift + override fun setPanHorizontalPadding(padding: Int) { + panManager.panHorizontalPadding = padding } override fun setOverpanFactor(factor: Float) { diff --git a/library/src/main/java/com/otaliastudios/zoom/ZoomImageView.kt b/library/src/main/java/com/otaliastudios/zoom/ZoomImageView.kt index 49dcc8e..dc91471 100644 --- a/library/src/main/java/com/otaliastudios/zoom/ZoomImageView.kt +++ b/library/src/main/java/com/otaliastudios/zoom/ZoomImageView.kt @@ -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 /** @@ -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) @@ -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 diff --git a/library/src/main/java/com/otaliastudios/zoom/ZoomLayout.kt b/library/src/main/java/com/otaliastudios/zoom/ZoomLayout.kt index 8c38e74..115284c 100644 --- a/library/src/main/java/com/otaliastudios/zoom/ZoomLayout.kt +++ b/library/src/main/java/com/otaliastudios/zoom/ZoomLayout.kt @@ -67,8 +67,8 @@ 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 horizontalShift = a.getInt(R.styleable.ZoomEngine_panHorizontalShift, ZoomApi.PAN_HORIZONTAL_SHIFT_DEFAULT) - val verticalShift = a.getInt(R.styleable.ZoomEngine_panVerticalShift, ZoomApi.PAN_VERTICAL_SHIFT_DEFAULT) + 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() @@ -95,8 +95,8 @@ open class ZoomLayout private constructor( setMinZoom(minZoom, minZoomMode) setMaxZoom(maxZoom, maxZoomMode) setHasClickableChildren(hasChildren) - setPanHorizontalShift(horizontalShift) - setPanVeritcalShift(verticalShift) + setPanHorizontalPadding(horizontalPadding) + setPanVerticalPadding(verticalPadding) setOverpanFactor(overpanFactor) setWillNotDraw(false) diff --git a/library/src/main/java/com/otaliastudios/zoom/ZoomSurfaceView.kt b/library/src/main/java/com/otaliastudios/zoom/ZoomSurfaceView.kt index db0903e..1041946 100644 --- a/library/src/main/java/com/otaliastudios/zoom/ZoomSurfaceView.kt +++ b/library/src/main/java/com/otaliastudios/zoom/ZoomSurfaceView.kt @@ -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 @@ -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 @@ -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) @@ -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) diff --git a/library/src/main/java/com/otaliastudios/zoom/internal/movement/PanManager.kt b/library/src/main/java/com/otaliastudios/zoom/internal/movement/PanManager.kt index 6b506fa..b5b39ce 100644 --- a/library/src/main/java/com/otaliastudios/zoom/internal/movement/PanManager.kt +++ b/library/src/main/java/com/otaliastudios/zoom/internal/movement/PanManager.kt @@ -25,8 +25,8 @@ internal class PanManager(provider: () -> MatrixController) : MovementManager(pr internal var horizontalPanEnabled = true internal var verticalPanEnabled = true internal var alignment = ZoomApi.ALIGNMENT_DEFAULT - internal var panVerticalShift = 0 - internal var panHorizontalShift = 0 + internal var panVerticalPadding = 0 + internal var panHorizontalPadding = 0 internal var overpanFactor = DEFAULT_OVERPAN_FACTOR /** whether overpan is enabled, horizontally or vertically */ @@ -64,8 +64,8 @@ internal class PanManager(provider: () -> MatrixController) : MovementManager(pr val alignment = if (horizontal) Alignment.getHorizontal(alignment) else Alignment.getVertical(alignment) @Suppress("CascadeIf") if (contentDim > containerDim && - panHorizontalShift == ZoomApi.PAN_HORIZONTAL_SHIFT_DEFAULT && - panVerticalShift == ZoomApi.PAN_VERTICAL_SHIFT_DEFAULT) { + 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 @@ -146,9 +146,8 @@ internal class PanManager(provider: () -> MatrixController) : MovementManager(pr } min -= overScroll max += overScroll -// shift = abs(shift) - val shift = if (horizontal) panHorizontalShift else panVerticalShift - val desired = value.coerceIn(min - shift, max + shift) + val padding = if (horizontal) panHorizontalPadding else panVerticalPadding + val desired = value.coerceIn(min - padding, max + padding) return desired - value } diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 4feea37..c0f5bd9 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -54,8 +54,8 @@ - - + +