From 0a247dd28b220ef83eb9e500e863b4e229195d34 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 4 Mar 2020 20:37:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../page/delegate/HorizontalPageDelegate.kt | 28 +++++++++++++++---- .../book/read/page/delegate/PageDelegate.kt | 16 +++++++---- .../read/page/delegate/ScrollPageDelegate.kt | 24 ++++++++++++++-- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt index 74583bae0..b10503fc8 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt @@ -4,7 +4,6 @@ import android.graphics.Bitmap import android.view.MotionEvent import io.legado.app.ui.book.read.page.PageView import io.legado.app.utils.screenshot -import kotlin.math.abs abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageView) { @@ -52,12 +51,31 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie } private fun onScroll(event: MotionEvent) { + + val action: Int = event.action + val pointerUp = + action and MotionEvent.ACTION_MASK == MotionEvent.ACTION_POINTER_UP + val skipIndex = if (pointerUp) event.actionIndex else -1 + // Determine focal point + var sumX = 0f + var sumY = 0f + val count: Int = event.pointerCount + for (i in 0 until count) { + if (skipIndex == i) continue + sumX += event.getX(i) + sumY += event.getY(i) + } + val div = if (pointerUp) count - 1 else count + val focusX = sumX / div + val focusY = sumY / div //判断是否移动了 if (!isMoved) { - isMoved = abs(startX - event.x) > slop - || abs(startX - event.x) > abs(startY - event.y) + val deltaX = (focusX - startX).toInt() + val deltaY = (focusY - startY).toInt() + val distance = deltaX * deltaX + deltaY * deltaY + isMoved = distance > slopSquare if (isMoved) { - if (event.x - startX > 0) { + if (sumX - startX > 0) { //如果上一页不存在 if (!hasPrev()) { noNext = true @@ -78,7 +96,7 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie isCancel = if (mDirection == Direction.NEXT) touchX > lastX else touchX < lastX isRunning = true //设置触摸点 - setTouchPoint(event.x, event.y) + setTouchPoint(sumX, sumY) } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt index 55a0272f9..d28f8c816 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt @@ -23,7 +23,7 @@ abstract class PageDelegate(protected val pageView: PageView) : pageView.width * 0.66f, pageView.height * 0.66f ) protected val context: Context = pageView.context - protected val slop = ViewConfiguration.get(context).scaledTouchSlop + //起始点 protected var startX: Float = 0f protected var startY: Float = 0f @@ -41,18 +41,23 @@ abstract class PageDelegate(protected val pageView: PageView) : protected var viewWidth: Int = pageView.width protected var viewHeight: Int = pageView.height - private val snackBar: Snackbar by lazy { - Snackbar.make(pageView, "", Snackbar.LENGTH_SHORT) - } - private val scroller: Scroller by lazy { Scroller(pageView.context, DecelerateInterpolator()) } + protected val slopSquare by lazy { + val scaledTouchSlop = ViewConfiguration.get(context).scaledTouchSlop + scaledTouchSlop * scaledTouchSlop + } + private val detector: GestureDetector by lazy { GestureDetector(pageView.context, this) } + private val snackBar: Snackbar by lazy { + Snackbar.make(pageView, "", Snackbar.LENGTH_SHORT) + } + var isMoved = false var noNext = true @@ -189,6 +194,7 @@ abstract class PageDelegate(protected val pageView: PageView) : @CallSuper open fun onTouch(event: MotionEvent) { if (isStarted) return + detector.setIsLongpressEnabled(false) if (!detector.onTouchEvent(event)) { //GestureDetector.onFling小幅移动不会触发,所以要自己判断 when (event.action) { diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/ScrollPageDelegate.kt b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/ScrollPageDelegate.kt index 0e5fb7dff..3fa333cbf 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/ScrollPageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/ScrollPageDelegate.kt @@ -4,7 +4,6 @@ import android.view.MotionEvent import android.view.VelocityTracker import io.legado.app.ui.book.read.page.ChapterProvider import io.legado.app.ui.book.read.page.PageView -import kotlin.math.abs class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) { @@ -46,9 +45,28 @@ class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) { private fun onScroll(event: MotionEvent) { mVelocity.addMovement(event) mVelocity.computeCurrentVelocity(velocityDuration) - setTouchPoint(event.x, event.y) + val action: Int = event.action + val pointerUp = + action and MotionEvent.ACTION_MASK == MotionEvent.ACTION_POINTER_UP + val skipIndex = if (pointerUp) event.actionIndex else -1 + // Determine focal point + var sumX = 0f + var sumY = 0f + val count: Int = event.pointerCount + for (i in 0 until count) { + if (skipIndex == i) continue + sumX += event.getX(i) + sumY += event.getY(i) + } + val div = if (pointerUp) count - 1 else count + val focusX = sumX / div + val focusY = sumY / div + setTouchPoint(sumX, sumY) if (!isMoved) { - isMoved = abs(startX - event.x) > slop || abs(startY - event.y) > slop + val deltaX = (focusX - startX).toInt() + val deltaY = (focusY - startY).toInt() + val distance = deltaX * deltaX + deltaY * deltaY + isMoved = distance > slopSquare } if (isMoved) { isRunning = true