From a2f49c7c0b2c17bb67ba9055e57033088f769b0c Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 22 Sep 2019 17:05:52 +0800 Subject: [PATCH] up --- .../app/ui/widget/page/TextPageFactory.kt | 10 +-- .../ui/widget/page/delegate/PageDelegate.kt | 39 ++++++++++-- .../page/delegate/ScrollPageDelegate.kt | 61 ++++++++++++++++++- 3 files changed, 100 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt b/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt index 6882dcae0..a69e37147 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt @@ -39,7 +39,9 @@ class TextPageFactory private constructor(dataSource: DataSource) : override fun moveToNext(): Boolean = dataSource.pageIndex().let { index -> return if (hasNext()) { - if (dataSource.getCurrentChapter()?.isLastIndex(index) == true) { + if (dataSource.getCurrentChapter()?.isLastIndex(index) == true + || dataSource.isScrollDelegate() + ) { dataSource.moveToNextChapter() } else { dataSource.setPageIndex(index.plus(1)) @@ -51,10 +53,10 @@ class TextPageFactory private constructor(dataSource: DataSource) : override fun moveToPrevious(): Boolean = dataSource.pageIndex().let { index -> return if (hasPrev()) { - if (index > 0) { - dataSource.setPageIndex(index.minus(1)) - } else { + if (index <= 0 || dataSource.isScrollDelegate()) { dataSource.moveToPrevChapter() + } else { + dataSource.setPageIndex(index.minus(1)) } true } else 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 a3352af33..5a51d580c 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 @@ -176,7 +176,7 @@ abstract class PageDelegate(protected val pageView: PageView) { /** * 触摸事件处理 */ - fun onTouch(event: MotionEvent): Boolean { + open fun onTouch(event: MotionEvent): Boolean { if (isStarted) return false if (curPage?.isTextSelected() == true) { curPage?.dispatchTouchEvent(event) @@ -278,8 +278,35 @@ abstract class PageDelegate(protected val pageView: PageView) { if (pageView.isScrollDelegate()) { //传递触摸事件到textView curPage?.dispatchTouchEvent(e2) - } - if (!isMoved && abs(distanceX) > abs(distanceY)) { + if (!isMoved && abs(distanceX) < abs(distanceY)) { + if (distanceY < 0) { + //上一页的参数配置 + direction = Direction.PREV + //判断是否上一页存在 + val hasPrev = pageView.hasPrev() + //如果上一页不存在 + if (!hasPrev) { + noNext = true + return true + } + //上一页截图 + bitmap = prevPage?.screenshot() + } else { + //进行下一页的配置 + direction = Direction.NEXT + //判断是否下一页存在 + val hasNext = pageView.hasNext() + //如果不存在表示没有下一页了 + if (!hasNext) { + noNext = true + return true + } + //下一页截图 + bitmap = nextPage?.screenshot() + } + isMoved = true + } + } else if (!isMoved && abs(distanceX) > abs(distanceY)) { if (distanceX < 0) { //上一页的参数配置 direction = Direction.PREV @@ -308,7 +335,11 @@ abstract class PageDelegate(protected val pageView: PageView) { isMoved = true } if (isMoved) { - isCancel = if (direction == Direction.NEXT) distanceX < 0 else distanceX > 0 + 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) 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 2da678320..0551673fb 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 @@ -1,21 +1,78 @@ 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 class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) { + private var atTop: Boolean = false + private var atBottom: Boolean = false + private val bitmapMatrix = Matrix() + + override fun onTouch(event: MotionEvent): Boolean { + if (event.action == MotionEvent.ACTION_DOWN) { + curPage?.contentTextView()?.let { + atTop = it.atTop() + atBottom = it.atBottom() + } + } + return super.onTouch(event) + } override fun onScrollStart() { + if (!atTop && !atBottom) { + stopScroll() + return + } + val distanceY: Float + when (direction) { + Direction.NEXT -> if (isCancel) { + var dis = viewHeight - startY + touchY + if (dis > viewHeight) { + dis = viewHeight.toFloat() + } + distanceY = viewHeight - dis + } else { + distanceY = -(touchY + (viewHeight - startY)) + } + else -> distanceY = if (isCancel) { + -(touchY - startY) + } else { + viewWidth - (touchY - startY) + } + } + startScroll(0, touchY.toInt(), 0, distanceY.toInt()) } override fun onDraw(canvas: Canvas) { + if (atTop || atBottom) { + val offsetY = touchY - startY + + if ((direction == Direction.NEXT && offsetY > 0) + || (direction == Direction.PREV && offsetY < 0) + ) return + val distanceY = if (offsetY > 0) offsetY - viewHeight else offsetY + viewHeight + if (atTop && direction == Direction.PREV) { + bitmap?.let { + bitmapMatrix.setTranslate(0.toFloat(), distanceY) + canvas.drawBitmap(it, bitmapMatrix, null) + } + } else if (atBottom && direction == Direction.NEXT) { + bitmap?.let { + bitmapMatrix.setTranslate(0.toFloat(), distanceY) + canvas.drawBitmap(it, bitmapMatrix, null) + } + } + } } override fun onScrollStop() { - + if (!isCancel) { + pageView.fillPage(direction) + } } - } \ No newline at end of file