From 197393fbd155d4aa77a844fddefcfdc3a08a83c7 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sat, 15 Jan 2022 17:21:03 +0200 Subject: [PATCH] Fix webtoon scroll --- app/build.gradle | 4 +-- .../ui/pager/webtoon/WebtoonImageView.kt | 33 +++++++++++++++++-- .../ui/pager/webtoon/WebtoonRecyclerView.kt | 2 +- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 38514e8a5..68bb3d879 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -13,8 +13,8 @@ android { applicationId 'org.koitharu.kotatsu' minSdkVersion 21 targetSdkVersion 31 - versionCode 377 - versionName '2.1.1' + versionCode 378 + versionName '2.1.2' generatedDensities = [] testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/src/main/java/org/koitharu/kotatsu/reader/ui/pager/webtoon/WebtoonImageView.kt b/app/src/main/java/org/koitharu/kotatsu/reader/ui/pager/webtoon/WebtoonImageView.kt index 09cb044c0..a762e05ef 100644 --- a/app/src/main/java/org/koitharu/kotatsu/reader/ui/pager/webtoon/WebtoonImageView.kt +++ b/app/src/main/java/org/koitharu/kotatsu/reader/ui/pager/webtoon/WebtoonImageView.kt @@ -1,16 +1,19 @@ package org.koitharu.kotatsu.reader.ui.pager.webtoon +import android.app.Activity import android.content.Context import android.graphics.PointF import android.util.AttributeSet import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView import org.koitharu.kotatsu.utils.ext.toIntUp -class WebtoonImageView @JvmOverloads constructor(context: Context, attr: AttributeSet? = null) : - SubsamplingScaleImageView(context, attr) { +class WebtoonImageView @JvmOverloads constructor( + context: Context, + attr: AttributeSet? = null, +) : SubsamplingScaleImageView(context, attr) { private val ct = PointF() - private val displayHeight = resources.displayMetrics.heightPixels + private val displayHeight = (context as Activity).window.decorView.height private var scrollPos = 0 private var scrollRange = SCROLL_UNKNOWN @@ -55,6 +58,30 @@ class WebtoonImageView @JvmOverloads constructor(context: Context, attr: Attribu return desiredHeight } + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + val widthSpecMode = MeasureSpec.getMode(widthMeasureSpec) + val heightSpecMode = MeasureSpec.getMode(heightMeasureSpec) + val parentWidth = MeasureSpec.getSize(widthMeasureSpec) + val parentHeight = MeasureSpec.getSize(heightMeasureSpec) + val resizeWidth = widthSpecMode != MeasureSpec.EXACTLY + val resizeHeight = heightSpecMode != MeasureSpec.EXACTLY + var width = parentWidth + var height = parentHeight + if (sWidth > 0 && sHeight > 0) { + if (resizeWidth && resizeHeight) { + width = sWidth + height = sHeight + } else if (resizeHeight) { + height = (sHeight.toDouble() / sWidth.toDouble() * width).toInt() + } else if (resizeWidth) { + width = (sWidth.toDouble() / sHeight.toDouble() * height).toInt() + } + } + width = width.coerceAtLeast(suggestedMinimumWidth) + height = height.coerceIn(suggestedMinimumHeight, displayHeight) + setMeasuredDimension(width, height) + } + private fun scrollToInternal(pos: Int) { scrollPos = pos ct.set(sWidth / 2f, (height / 2f + pos.toFloat()) / minScale) diff --git a/app/src/main/java/org/koitharu/kotatsu/reader/ui/pager/webtoon/WebtoonRecyclerView.kt b/app/src/main/java/org/koitharu/kotatsu/reader/ui/pager/webtoon/WebtoonRecyclerView.kt index f4535d7be..dfb0bad8c 100644 --- a/app/src/main/java/org/koitharu/kotatsu/reader/ui/pager/webtoon/WebtoonRecyclerView.kt +++ b/app/src/main/java/org/koitharu/kotatsu/reader/ui/pager/webtoon/WebtoonRecyclerView.kt @@ -34,7 +34,7 @@ class WebtoonRecyclerView @JvmOverloads constructor( consumed[0] = 0 consumed[1] = consumedY } - return consumedY != 0 + return consumedY != 0 || dy == 0 } private fun consumeVerticalScroll(dy: Int): Int {