improve bounds checking at gif view

This commit is contained in:
tibbi 2019-01-16 14:02:21 +01:00
parent 494745c10f
commit 0e68d01f08

View file

@ -34,6 +34,7 @@ class MyZoomableGifTextureView(context: Context, attrs: AttributeSet) : GifTextu
private val mMatrix = Matrix() private val mMatrix = Matrix()
private var mOrigRect = RectF() private var mOrigRect = RectF()
private var mWantedRect = RectF() private var mWantedRect = RectF()
private var mCurrentViewport = RectF()
init { init {
mScaleDetector = ScaleGestureDetector(context, ScaleListener()) mScaleDetector = ScaleGestureDetector(context, ScaleListener())
@ -57,20 +58,21 @@ class MyZoomableGifTextureView(context: Context, attrs: AttributeSet) : GifTextu
mOrigRect = RectF(0f, 0f, mGifWidth, mGifHeight) mOrigRect = RectF(0f, 0f, mGifWidth, mGifHeight)
mWantedRect = RectF(0f, 0f, mScreenWidth, mScreenHeight) mWantedRect = RectF(0f, 0f, mScreenWidth, mScreenHeight)
mMatrix.setRectToRect(mOrigRect, mWantedRect, Matrix.ScaleToFit.CENTER) 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) setTransform(mMatrix)
invalidate() invalidate()
beVisible() beVisible()
} }
override fun onTouchEvent(event: MotionEvent): Boolean { override fun onTouchEvent(event: MotionEvent): Boolean {
mScaleDetector?.onTouchEvent(event) // 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]
when (event.actionMasked) { when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> { 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) { if (mCurrZoomMode == ZOOM_MODE_ZOOM || mCurrZoomMode == ZOOM_MODE_DRAG && mSaveScale > MIN_VIDEO_ZOOM_SCALE) {
var diffX = event.x - mLastTouchX var diffX = event.x - mLastTouchX
var diffY = event.y - mLastTouchY var diffY = event.y - mLastTouchY
if (y + diffY > 0) {
diffY = -y // horizontal bounds
} else if (y + diffY < -mBottom) { if (mCurrentViewport.left + diffX < 0) {
diffY = -(y + mBottom) diffX = -mCurrentViewport.left
} else if (mCurrentViewport.right + diffX > mScreenWidth) {
diffX = mScreenWidth - mCurrentViewport.right
} }
if (x + diffX > 0) { // vertical bounds
diffX = -x if (mCurrentViewport.top + diffY < 0) {
} else if (x + diffX < -mRight) { diffY = -mCurrentViewport.top
diffX = -(x + mRight) } 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) 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 mLastTouchX = event.x
mLastTouchY = event.y mLastTouchY = event.y
} }