Skip to content

Commit

Permalink
Pinch to zoom bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
afreakyelf committed Feb 27, 2024
1 parent 93d8c0d commit 9a5fd88
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* Created by Rajat on 11,July,2020
*/

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
internal class PdfRendererCore(
private val context: Context,
fileDescriptor: ParcelFileDescriptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,50 @@ class PinchZoomRecyclerView : RecyclerView {
}

private inner class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener() {
// override fun onScale(detector: ScaleGestureDetector): Boolean {
// val scaleFactor =
// 1f.coerceAtLeast((mScaleFactor * detector.scaleFactor).coerceAtMost(MAX_SCALE))
// val focusX = detector.focusX
// val focusY = detector.focusY
//
// if (scaleFactor != mScaleFactor) {
// val scaleDelta = scaleFactor / mScaleFactor
// mPosX -= (focusX - mPosX) * (1 - scaleDelta)
// mPosY -= (focusY - mPosY) * (1 - scaleDelta)
// mScaleFactor = scaleFactor
//
// mPosX = (maxWidth - width * mScaleFactor).coerceAtLeast(mPosX.coerceAtMost(0f))
// mPosY = (maxHeight - height * mScaleFactor).coerceAtLeast(mPosY.coerceAtMost(0f))
//
// invalidate()
// }
//
// return true
// }


override fun onScale(detector: ScaleGestureDetector): Boolean {
val scaleFactor =
1f.coerceAtLeast((mScaleFactor * detector.scaleFactor).coerceAtMost(MAX_SCALE))
val scaleFactor = 1f.coerceAtLeast((mScaleFactor * detector.scaleFactor).coerceAtMost(MAX_SCALE))
val focusX = detector.focusX
val focusY = detector.focusY

if (scaleFactor != mScaleFactor) {
val scaleDelta = scaleFactor / mScaleFactor
mPosX -= (focusX - mPosX) * (1 - scaleDelta)
mPosY -= (focusY - mPosY) * (1 - scaleDelta)
mScaleFactor = scaleFactor

mPosX = (maxWidth - width * mScaleFactor).coerceAtLeast(mPosX.coerceAtMost(0f))
mPosY = (maxHeight - height * mScaleFactor).coerceAtLeast(mPosY.coerceAtMost(0f))
// Adjust position so that it scales from the pinch zoom center
mPosX += (focusX - mPosX) * (1 - scaleDelta)
mPosY += (focusY - mPosY) * (1 - scaleDelta)

invalidate()
// Update the scale factor
mScaleFactor = scaleFactor

// Make sure the view is within bounds
clampPosition() // You may need to update this method as well if necessary
}

return true
}

}
private fun resetZoom() {
mScaleFactor = 1f
Expand Down

0 comments on commit 9a5fd88

Please sign in to comment.