From beb5850010f0ff87c02b490eb14d59d88ba1e758 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 6 Oct 2019 19:18:17 +0800 Subject: [PATCH] up --- .../widget/page/delegate/CoverPageDelegate.kt | 2 +- .../page/delegate/HorizontalPageDelegate.kt | 53 +++++++++++ .../page/delegate/NoAnimPageDelegate.kt | 2 +- .../ui/widget/page/delegate/PageDelegate.kt | 87 +++---------------- .../page/delegate/ScrollPageDelegate.kt | 56 ++++++++++++ .../page/delegate/SimulationPageDelegate.kt | 4 +- .../widget/page/delegate/SlidePageDelegate.kt | 2 +- 7 files changed, 127 insertions(+), 79 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/widget/page/delegate/HorizontalPageDelegate.kt diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/CoverPageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/CoverPageDelegate.kt index dd4f8d185..10ac07abd 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/delegate/CoverPageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/CoverPageDelegate.kt @@ -5,7 +5,7 @@ import android.graphics.Matrix import android.graphics.drawable.GradientDrawable import io.legado.app.ui.widget.page.PageView -class CoverPageDelegate(pageView: PageView) : PageDelegate(pageView) { +class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) { private val shadowDrawableR: GradientDrawable private val bitmapMatrix = Matrix() diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/HorizontalPageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/HorizontalPageDelegate.kt new file mode 100644 index 000000000..1f2b3fa42 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/HorizontalPageDelegate.kt @@ -0,0 +1,53 @@ +package io.legado.app.ui.widget.page.delegate + +import android.view.MotionEvent +import io.legado.app.ui.widget.page.PageView +import io.legado.app.utils.screenshot +import kotlin.math.abs + +abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageView) { + + override fun onScroll( + e1: MotionEvent, + e2: MotionEvent, + distanceX: Float, + distanceY: Float + ): Boolean { + if (!isMoved) { + val event = e1.toAction(MotionEvent.ACTION_UP) + curPage?.dispatchTouchEvent(event) + event.recycle() + if (abs(distanceX) > abs(distanceY)) { + if (distanceX < 0) { + //如果上一页不存在 + if (!hasPrev()) { + noNext = true + return true + } + //上一页截图 + bitmap = prevPage?.screenshot() + } else { + //如果不存在表示没有下一页了 + if (!hasNext()) { + noNext = true + return true + } + //下一页截图 + bitmap = nextPage?.screenshot() + } + isMoved = true + } + } + if (isMoved) { + isCancel = if (pageView.isScrollDelegate) { + if (direction == Direction.NEXT) distanceY < 0 else distanceY > 0 + } else { + if (direction == Direction.NEXT) distanceX < 0 else distanceX > 0 + } + isRunning = true + //设置触摸点 + setTouchPoint(e2.x, e2.y) + } + return isMoved + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/NoAnimPageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/NoAnimPageDelegate.kt index 7d19a25ed..7008455b8 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/delegate/NoAnimPageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/NoAnimPageDelegate.kt @@ -3,7 +3,7 @@ package io.legado.app.ui.widget.page.delegate import android.graphics.Canvas import io.legado.app.ui.widget.page.PageView -class NoAnimPageDelegate(pageView: PageView) : PageDelegate(pageView) { +class NoAnimPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) { override fun onScrollStart() { startScroll(touchX.toInt(), 0, 0, 0) } diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt index a59a6a86b..b70a1ac6d 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt @@ -56,8 +56,8 @@ abstract class PageDelegate(protected val pageView: PageView) { ) } - private var isMoved = false - private var noNext = true + var isMoved = false + var noNext = true //移动方向 var direction = Direction.NONE @@ -212,6 +212,13 @@ abstract class PageDelegate(protected val pageView: PageView) { open fun onPageUp() { } + abstract fun onScroll( + e1: MotionEvent, + e2: MotionEvent, + distanceX: Float, + distanceY: Float + ): Boolean + enum class Direction { NONE, PREV, NEXT } @@ -271,87 +278,17 @@ abstract class PageDelegate(protected val pageView: PageView) { distanceX: Float, distanceY: Float ): Boolean { - if (pageView.isScrollDelegate) { - if (!isMoved && abs(distanceX) < abs(distanceY)) { - if (distanceY < 0) { - if (atTop) { - val event = e1.toAction(MotionEvent.ACTION_UP) - curPage?.dispatchTouchEvent(event) - event.recycle() - //如果上一页不存在 - if (!hasPrev()) { - noNext = true - return true - } - //上一页截图 - bitmap = prevPage?.screenshot() - } - } else { - if (atBottom) { - val event = e1.toAction(MotionEvent.ACTION_UP) - curPage?.dispatchTouchEvent(event) - event.recycle() - //如果不存在表示没有下一页了 - if (!hasNext()) { - noNext = true - return true - } - //下一页截图 - bitmap = nextPage?.screenshot() - } - } - isMoved = true - } - if ((atTop && direction != Direction.PREV) || (atBottom && direction != Direction.NEXT) || direction == Direction.NONE) { - //传递触摸事件到textView - curPage?.dispatchTouchEvent(e2) - } - } else if (!isMoved) { - val event = e1.toAction(MotionEvent.ACTION_UP) - curPage?.dispatchTouchEvent(event) - event.recycle() - if (abs(distanceX) > abs(distanceY)) { - if (distanceX < 0) { - //如果上一页不存在 - if (!hasPrev()) { - noNext = true - return true - } - //上一页截图 - bitmap = prevPage?.screenshot() - } else { - //如果不存在表示没有下一页了 - if (!hasNext()) { - noNext = true - return true - } - //下一页截图 - bitmap = nextPage?.screenshot() - } - isMoved = true - } - } - if (isMoved) { - isCancel = if (pageView.isScrollDelegate) { - if (direction == Direction.NEXT) distanceY < 0 else distanceY > 0 - } else { - if (direction == Direction.NEXT) distanceX < 0 else distanceX > 0 - } - isRunning = true - //设置触摸点 - setTouchPoint(e2.x, e2.y) - } - return isMoved + return this@PageDelegate.onScroll(e1, e2, distanceX, distanceY) } } - private fun hasPrev(): Boolean { + fun hasPrev(): Boolean { //上一页的参数配置 direction = Direction.PREV return pageView.pageFactory?.hasPrev() == true } - private fun hasNext(): Boolean { + fun hasNext(): Boolean { //进行下一页的配置 direction = Direction.NEXT return pageView.pageFactory?.hasNext() == true diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/ScrollPageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/ScrollPageDelegate.kt index a2d900529..4e333bd1c 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/delegate/ScrollPageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/ScrollPageDelegate.kt @@ -2,7 +2,10 @@ package io.legado.app.ui.widget.page.delegate import android.graphics.Canvas import android.graphics.Matrix +import android.view.MotionEvent import io.legado.app.ui.widget.page.PageView +import io.legado.app.utils.screenshot +import kotlin.math.abs class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) { @@ -65,4 +68,57 @@ class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) { } } + override fun onScroll( + e1: MotionEvent, + e2: MotionEvent, + distanceX: Float, + distanceY: Float + ): Boolean { + if (!isMoved && abs(distanceX) < abs(distanceY)) { + if (distanceY < 0) { + if (atTop) { + val event = e1.toAction(MotionEvent.ACTION_UP) + curPage?.dispatchTouchEvent(event) + event.recycle() + //如果上一页不存在 + if (!hasPrev()) { + noNext = true + return true + } + //上一页截图 + bitmap = prevPage?.screenshot() + } + } else { + if (atBottom) { + val event = e1.toAction(MotionEvent.ACTION_UP) + curPage?.dispatchTouchEvent(event) + event.recycle() + //如果不存在表示没有下一页了 + if (!hasNext()) { + noNext = true + return true + } + //下一页截图 + bitmap = nextPage?.screenshot() + } + } + isMoved = true + } + if ((atTop && direction != Direction.PREV) || (atBottom && direction != Direction.NEXT) || direction == Direction.NONE) { + //传递触摸事件到textView + curPage?.dispatchTouchEvent(e2) + } + if (isMoved) { + isCancel = if (pageView.isScrollDelegate) { + if (direction == Direction.NEXT) distanceY < 0 else distanceY > 0 + } else { + if (direction == Direction.NEXT) distanceX < 0 else distanceX > 0 + } + isRunning = true + //设置触摸点 + setTouchPoint(e2.x, e2.y) + } + return isMoved + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/SimulationPageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/SimulationPageDelegate.kt index 7722b790e..9b42a3edd 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/delegate/SimulationPageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/SimulationPageDelegate.kt @@ -5,13 +5,15 @@ import android.view.MotionEvent import io.legado.app.ui.widget.page.PageView import io.legado.app.ui.widget.page.curl.CurlPage import io.legado.app.ui.widget.page.curl.CurlView +import io.legado.app.utils.invisible import io.legado.app.utils.screenshot -class SimulationPageDelegate(pageView: PageView) : PageDelegate(pageView) { +class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) { init { pageView.curlView ?: let { pageView.curlView = CurlView(pageView.context) + pageView.curlView?.invisible() pageView.addView(pageView.curlView) pageView.curlView?.mPageProvider = PageProvider() pageView.curlView?.setSizeChangedObserver(SizeChangedObserver()) diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/SlidePageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/SlidePageDelegate.kt index 60cc351f6..2615976e6 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/delegate/SlidePageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/SlidePageDelegate.kt @@ -4,7 +4,7 @@ import android.graphics.Canvas import android.graphics.Matrix import io.legado.app.ui.widget.page.PageView -class SlidePageDelegate(pageView: PageView) : PageDelegate(pageView) { +class SlidePageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) { private val bitmapMatrix = Matrix()