From 0e68d01f08b10a27a858559586bbc15173c503eb Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 16 Jan 2019 14:02:21 +0100 Subject: [PATCH] improve bounds checking at gif view --- .../pro/views/MyZoomableGifTextureView.kt | 48 ++++++++++++------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/views/MyZoomableGifTextureView.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/views/MyZoomableGifTextureView.kt index 59aec6067..01443ce52 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/views/MyZoomableGifTextureView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/views/MyZoomableGifTextureView.kt @@ -34,6 +34,7 @@ class MyZoomableGifTextureView(context: Context, attrs: AttributeSet) : GifTextu private val mMatrix = Matrix() private var mOrigRect = RectF() private var mWantedRect = RectF() + private var mCurrentViewport = RectF() init { mScaleDetector = ScaleGestureDetector(context, ScaleListener()) @@ -57,20 +58,21 @@ class MyZoomableGifTextureView(context: Context, attrs: AttributeSet) : GifTextu mOrigRect = RectF(0f, 0f, mGifWidth, mGifHeight) mWantedRect = RectF(0f, 0f, mScreenWidth, mScreenHeight) mMatrix.setRectToRect(mOrigRect, mWantedRect, Matrix.ScaleToFit.CENTER) + mMatrix.getValues(mMatrices) + + val left = mMatrices[Matrix.MTRANS_X] + val top = mMatrices[Matrix.MTRANS_Y] + val right = mScreenWidth - left + val bottom = mScreenHeight - top + mCurrentViewport.set(left, top, right, bottom) + setTransform(mMatrix) invalidate() beVisible() } override fun onTouchEvent(event: MotionEvent): Boolean { - mScaleDetector?.onTouchEvent(event) - - mMatrix.getValues(mMatrices) - mMatrix.setRectToRect(mOrigRect, mWantedRect, Matrix.ScaleToFit.CENTER) - mMatrix.postScale(mSaveScale, mSaveScale, mLastFocusX, mLastFocusY) - - val x = mMatrices[Matrix.MTRANS_X] - val y = mMatrices[Matrix.MTRANS_Y] +// mScaleDetector?.onTouchEvent(event) when (event.actionMasked) { MotionEvent.ACTION_DOWN -> { @@ -97,19 +99,33 @@ class MyZoomableGifTextureView(context: Context, attrs: AttributeSet) : GifTextu if (mCurrZoomMode == ZOOM_MODE_ZOOM || mCurrZoomMode == ZOOM_MODE_DRAG && mSaveScale > MIN_VIDEO_ZOOM_SCALE) { var diffX = event.x - mLastTouchX var diffY = event.y - mLastTouchY - if (y + diffY > 0) { - diffY = -y - } else if (y + diffY < -mBottom) { - diffY = -(y + mBottom) + + // horizontal bounds + if (mCurrentViewport.left + diffX < 0) { + diffX = -mCurrentViewport.left + } else if (mCurrentViewport.right + diffX > mScreenWidth) { + diffX = mScreenWidth - mCurrentViewport.right } - if (x + diffX > 0) { - diffX = -x - } else if (x + diffX < -mRight) { - diffX = -(x + mRight) + // vertical bounds + if (mCurrentViewport.top + diffY < 0) { + diffY = -mCurrentViewport.top + } else if (mCurrentViewport.bottom + diffY > mScreenHeight) { + diffY = mScreenHeight - mCurrentViewport.bottom } + mCurrentViewport.left += diffX + mCurrentViewport.right += diffX + mCurrentViewport.top += diffY + mCurrentViewport.bottom += diffY + mMatrix.postTranslate(diffX, diffY) + + mCurrentViewport.left = Math.max(mCurrentViewport.left, 0f) + mCurrentViewport.right = Math.min(mCurrentViewport.right, mScreenWidth) + mCurrentViewport.top = Math.max(mCurrentViewport.top, 0f) + mCurrentViewport.bottom = Math.min(mCurrentViewport.bottom, mScreenHeight) + mLastTouchX = event.x mLastTouchY = event.y }