From 4fc8800a371d6de0ca2e07187e163965485096ce Mon Sep 17 00:00:00 2001 From: morcefaster <51055657+morcefaster@users.noreply.github.com> Date: Sat, 18 Apr 2020 21:43:54 +0300 Subject: [PATCH] Webtoon zoom out (#2892) * Increased added support for zoom out on webtoons to help with horizontal layout reading * Renamed var --- .../viewer/webtoon/WebtoonRecyclerView.kt | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonRecyclerView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonRecyclerView.kt index c989163970..73dcd23f19 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonRecyclerView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonRecyclerView.kt @@ -28,6 +28,8 @@ open class WebtoonRecyclerView @JvmOverloads constructor( private var atFirstPosition = false private var halfWidth = 0 private var halfHeight = 0 + private var originalHeight = 0 + private var heightSet = false private var firstVisibleItemPosition = 0 private var lastVisibleItemPosition = 0 private var currentScale = DEFAULT_RATE @@ -41,6 +43,10 @@ open class WebtoonRecyclerView @JvmOverloads constructor( override fun onMeasure(widthSpec: Int, heightSpec: Int) { halfWidth = MeasureSpec.getSize(widthSpec) / 2 halfHeight = MeasureSpec.getSize(heightSpec) / 2 + if (!heightSet) { + originalHeight = MeasureSpec.getSize(heightSpec) + heightSet = true + } super.onMeasure(widthSpec, heightSpec) } @@ -67,11 +73,17 @@ open class WebtoonRecyclerView @JvmOverloads constructor( } private fun getPositionX(positionX: Float): Float { + if (currentScale < 1) { + return 0f + } val maxPositionX = halfWidth * (currentScale - 1) return positionX.coerceIn(-maxPositionX, maxPositionX) } private fun getPositionY(positionY: Float): Float { + if (currentScale < 1) { + return (originalHeight / 2 - halfHeight).toFloat() + } val maxPositionY = halfHeight * (currentScale - 1) return positionY.coerceIn(-maxPositionY, maxPositionY) } @@ -162,11 +174,14 @@ open class WebtoonRecyclerView @JvmOverloads constructor( fun onScale(scaleFactor: Float) { currentScale *= scaleFactor currentScale = currentScale.coerceIn( - DEFAULT_RATE, + MIN_RATE, MAX_SCALE_RATE) setScaleRate(currentScale) + layoutParams.height = if (currentScale < 1) { (originalHeight / currentScale).toInt() } else { originalHeight } + halfHeight = layoutParams.height / 2 + if (currentScale != DEFAULT_RATE) { x = getPositionX(x) y = getPositionY(y) @@ -174,6 +189,8 @@ open class WebtoonRecyclerView @JvmOverloads constructor( x = 0f y = 0f } + + requestLayout() } fun onScaleBegin() { @@ -183,8 +200,8 @@ open class WebtoonRecyclerView @JvmOverloads constructor( } fun onScaleEnd() { - if (scaleX < DEFAULT_RATE) { - zoom(currentScale, DEFAULT_RATE, x, 0f, y, 0f) + if (scaleX < MIN_RATE) { + zoom(currentScale, MIN_RATE, x, 0f, y, 0f) } } @@ -310,6 +327,7 @@ open class WebtoonRecyclerView @JvmOverloads constructor( private companion object { const val ANIMATOR_DURATION_TIME = 200 + const val MIN_RATE = 0.5f const val DEFAULT_RATE = 1f const val MAX_SCALE_RATE = 3f }