Webtoon zoom out (#2892)

* Increased added support for zoom out on webtoons to help with horizontal layout reading

* Renamed var
This commit is contained in:
morcefaster 2020-04-18 21:43:54 +03:00 committed by GitHub
parent 6a532b836d
commit 4fc8800a37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,6 +28,8 @@ open class WebtoonRecyclerView @JvmOverloads constructor(
private var atFirstPosition = false private var atFirstPosition = false
private var halfWidth = 0 private var halfWidth = 0
private var halfHeight = 0 private var halfHeight = 0
private var originalHeight = 0
private var heightSet = false
private var firstVisibleItemPosition = 0 private var firstVisibleItemPosition = 0
private var lastVisibleItemPosition = 0 private var lastVisibleItemPosition = 0
private var currentScale = DEFAULT_RATE private var currentScale = DEFAULT_RATE
@ -41,6 +43,10 @@ open class WebtoonRecyclerView @JvmOverloads constructor(
override fun onMeasure(widthSpec: Int, heightSpec: Int) { override fun onMeasure(widthSpec: Int, heightSpec: Int) {
halfWidth = MeasureSpec.getSize(widthSpec) / 2 halfWidth = MeasureSpec.getSize(widthSpec) / 2
halfHeight = MeasureSpec.getSize(heightSpec) / 2 halfHeight = MeasureSpec.getSize(heightSpec) / 2
if (!heightSet) {
originalHeight = MeasureSpec.getSize(heightSpec)
heightSet = true
}
super.onMeasure(widthSpec, heightSpec) super.onMeasure(widthSpec, heightSpec)
} }
@ -67,11 +73,17 @@ open class WebtoonRecyclerView @JvmOverloads constructor(
} }
private fun getPositionX(positionX: Float): Float { private fun getPositionX(positionX: Float): Float {
if (currentScale < 1) {
return 0f
}
val maxPositionX = halfWidth * (currentScale - 1) val maxPositionX = halfWidth * (currentScale - 1)
return positionX.coerceIn(-maxPositionX, maxPositionX) return positionX.coerceIn(-maxPositionX, maxPositionX)
} }
private fun getPositionY(positionY: Float): Float { private fun getPositionY(positionY: Float): Float {
if (currentScale < 1) {
return (originalHeight / 2 - halfHeight).toFloat()
}
val maxPositionY = halfHeight * (currentScale - 1) val maxPositionY = halfHeight * (currentScale - 1)
return positionY.coerceIn(-maxPositionY, maxPositionY) return positionY.coerceIn(-maxPositionY, maxPositionY)
} }
@ -162,11 +174,14 @@ open class WebtoonRecyclerView @JvmOverloads constructor(
fun onScale(scaleFactor: Float) { fun onScale(scaleFactor: Float) {
currentScale *= scaleFactor currentScale *= scaleFactor
currentScale = currentScale.coerceIn( currentScale = currentScale.coerceIn(
DEFAULT_RATE, MIN_RATE,
MAX_SCALE_RATE) MAX_SCALE_RATE)
setScaleRate(currentScale) setScaleRate(currentScale)
layoutParams.height = if (currentScale < 1) { (originalHeight / currentScale).toInt() } else { originalHeight }
halfHeight = layoutParams.height / 2
if (currentScale != DEFAULT_RATE) { if (currentScale != DEFAULT_RATE) {
x = getPositionX(x) x = getPositionX(x)
y = getPositionY(y) y = getPositionY(y)
@ -174,6 +189,8 @@ open class WebtoonRecyclerView @JvmOverloads constructor(
x = 0f x = 0f
y = 0f y = 0f
} }
requestLayout()
} }
fun onScaleBegin() { fun onScaleBegin() {
@ -183,8 +200,8 @@ open class WebtoonRecyclerView @JvmOverloads constructor(
} }
fun onScaleEnd() { fun onScaleEnd() {
if (scaleX < DEFAULT_RATE) { if (scaleX < MIN_RATE) {
zoom(currentScale, DEFAULT_RATE, x, 0f, y, 0f) zoom(currentScale, MIN_RATE, x, 0f, y, 0f)
} }
} }
@ -310,6 +327,7 @@ open class WebtoonRecyclerView @JvmOverloads constructor(
private companion object { private companion object {
const val ANIMATOR_DURATION_TIME = 200 const val ANIMATOR_DURATION_TIME = 200
const val MIN_RATE = 0.5f
const val DEFAULT_RATE = 1f const val DEFAULT_RATE = 1f
const val MAX_SCALE_RATE = 3f const val MAX_SCALE_RATE = 3f
} }