diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index dc155cd10..227d6b9a7 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -37,7 +37,7 @@ import io.legado.app.ui.book.read.config.BgTextConfigDialog.Companion.TEXT_COLOR import io.legado.app.ui.book.read.page.ContentTextView import io.legado.app.ui.book.read.page.PageView import io.legado.app.ui.book.read.page.TextPageFactory -import io.legado.app.ui.book.read.page.delegate.PageDelegate +import io.legado.app.ui.book.read.page.entities.PageDirection import io.legado.app.ui.book.searchContent.SearchContentActivity import io.legado.app.ui.book.toc.ChapterListActivity import io.legado.app.ui.login.SourceLogin @@ -260,28 +260,28 @@ class ReadBookActivity : ReadBookBaseActivity(), when { isPrevKey(keyCode) -> { if (keyCode != KeyEvent.KEYCODE_UNKNOWN) { - page_view.pageDelegate?.keyTurnPage(PageDelegate.Direction.PREV) + page_view.pageDelegate?.keyTurnPage(PageDirection.PREV) return true } } isNextKey(keyCode) -> { if (keyCode != KeyEvent.KEYCODE_UNKNOWN) { - page_view.pageDelegate?.keyTurnPage(PageDelegate.Direction.NEXT) + page_view.pageDelegate?.keyTurnPage(PageDirection.NEXT) return true } } keyCode == KeyEvent.KEYCODE_VOLUME_UP -> { - if (volumeKeyPage(PageDelegate.Direction.PREV)) { + if (volumeKeyPage(PageDirection.PREV)) { return true } } keyCode == KeyEvent.KEYCODE_VOLUME_DOWN -> { - if (volumeKeyPage(PageDelegate.Direction.NEXT)) { + if (volumeKeyPage(PageDirection.NEXT)) { return true } } keyCode == KeyEvent.KEYCODE_SPACE -> { - page_view.pageDelegate?.keyTurnPage(PageDelegate.Direction.NEXT) + page_view.pageDelegate?.keyTurnPage(PageDirection.NEXT) return true } } @@ -307,7 +307,7 @@ class ReadBookActivity : ReadBookBaseActivity(), override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean { when (keyCode) { KeyEvent.KEYCODE_VOLUME_UP, KeyEvent.KEYCODE_VOLUME_DOWN -> { - if (volumeKeyPage(PageDelegate.Direction.NONE)) { + if (volumeKeyPage(PageDirection.NONE)) { return true } } @@ -458,7 +458,7 @@ class ReadBookActivity : ReadBookBaseActivity(), /** * 音量键翻页 */ - private fun volumeKeyPage(direction: PageDelegate.Direction): Boolean { + private fun volumeKeyPage(direction: PageDirection): Boolean { if (!read_menu.isVisible) { if (getPrefBoolean("volumeKeyPage", true)) { if (getPrefBoolean("volumeKeyPageOnPlay") @@ -612,7 +612,7 @@ class ReadBookActivity : ReadBookBaseActivity(), autoPageProgress++ if (autoPageProgress >= ReadBookConfig.autoReadSpeed * 50) { autoPageProgress = 0 - page_view.fillPage(PageDelegate.Direction.NEXT) + page_view.fillPage(PageDirection.NEXT) } else { page_view.invalidate() } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt index 4abc569e7..ae5edd4f5 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt @@ -15,6 +15,7 @@ import io.legado.app.help.ReadBookConfig import io.legado.app.lib.theme.accentColor import io.legado.app.service.help.ReadBook import io.legado.app.ui.book.read.page.delegate.* +import io.legado.app.ui.book.read.page.entities.PageDirection import io.legado.app.ui.book.read.page.entities.TextChapter import io.legado.app.ui.book.read.page.provider.ChapterProvider import io.legado.app.utils.activity @@ -339,12 +340,12 @@ class PageView(context: Context, attrs: AttributeSet) : curPage.cancelSelect() } - fun fillPage(direction: PageDelegate.Direction) { + fun fillPage(direction: PageDirection) { when (direction) { - PageDelegate.Direction.PREV -> { + PageDirection.PREV -> { pageFactory.moveToPrev(true) } - PageDelegate.Direction.NEXT -> { + PageDirection.NEXT -> { pageFactory.moveToNext(true) } else -> Unit diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/CoverPageDelegate.kt b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/CoverPageDelegate.kt index 278b8fffc..5a32fda2f 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/CoverPageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/CoverPageDelegate.kt @@ -4,6 +4,7 @@ import android.graphics.Canvas import android.graphics.Matrix import android.graphics.drawable.GradientDrawable import io.legado.app.ui.book.read.page.PageView +import io.legado.app.ui.book.read.page.entities.PageDirection class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) { private val bitmapMatrix = Matrix() @@ -21,19 +22,19 @@ class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) { if (!isRunning) return val offsetX = touchX - startX - if ((mDirection == Direction.NEXT && offsetX > 0) - || (mDirection == Direction.PREV && offsetX < 0) + if ((mDirection == PageDirection.NEXT && offsetX > 0) + || (mDirection == PageDirection.PREV && offsetX < 0) ) { return } val distanceX = if (offsetX > 0) offsetX - viewWidth else offsetX + viewWidth - if (mDirection == Direction.PREV) { + if (mDirection == PageDirection.PREV) { bitmapMatrix.setTranslate(distanceX, 0.toFloat()) curBitmap?.let { canvas.drawBitmap(it, 0f, 0f, null) } prevBitmap?.let { canvas.drawBitmap(it, bitmapMatrix, null) } addShadow(distanceX.toInt(), canvas) - } else if (mDirection == Direction.NEXT) { + } else if (mDirection == PageDirection.NEXT) { bitmapMatrix.setTranslate(distanceX - viewWidth, 0.toFloat()) nextBitmap?.let { canvas.drawBitmap(it, 0f, 0f, null) } curBitmap?.let { canvas.drawBitmap(it, bitmapMatrix, null) } @@ -60,7 +61,7 @@ class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) { override fun onAnimStart(animationSpeed: Int) { val distanceX: Float when (mDirection) { - Direction.NEXT -> distanceX = + PageDirection.NEXT -> distanceX = if (isCancel) { var dis = viewWidth - startX + touchX if (dis > viewWidth) { 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 356d7669c..e6d5d9ee5 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 @@ -3,6 +3,7 @@ package io.legado.app.ui.book.read.page.delegate import android.graphics.Bitmap import android.view.MotionEvent import io.legado.app.ui.book.read.page.PageView +import io.legado.app.ui.book.read.page.entities.PageDirection import io.legado.app.utils.screenshot abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageView) { @@ -11,20 +12,20 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie protected var prevBitmap: Bitmap? = null protected var nextBitmap: Bitmap? = null - override fun setDirection(direction: Direction) { + override fun setDirection(direction: PageDirection) { super.setDirection(direction) setBitmap() } private fun setBitmap() { when (mDirection) { - Direction.PREV -> { + PageDirection.PREV -> { prevBitmap?.recycle() prevBitmap = prevPage.screenshot() curBitmap?.recycle() curBitmap = curPage.screenshot() } - Direction.NEXT -> { + PageDirection.NEXT -> { nextBitmap?.recycle() nextBitmap = nextPage.screenshot() curBitmap?.recycle() @@ -79,19 +80,19 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie noNext = true return } - setDirection(Direction.PREV) + setDirection(PageDirection.PREV) } else { //如果不存在表示没有下一页了 if (!hasNext()) { noNext = true return } - setDirection(Direction.NEXT) + setDirection(PageDirection.NEXT) } } } if (isMoved) { - isCancel = if (mDirection == Direction.NEXT) sumX > lastX else sumX < lastX + isCancel = if (mDirection == PageDirection.NEXT) sumX > lastX else sumX < lastX isRunning = true //设置触摸点 pageView.setTouchPoint(sumX, sumY) @@ -117,7 +118,7 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie override fun nextPageByAnim(animationSpeed: Int) { abortAnim() if (!hasNext()) return - setDirection(Direction.NEXT) + setDirection(PageDirection.NEXT) pageView.setTouchPoint(viewWidth.toFloat(), 0f, false) onAnimStart(animationSpeed) } @@ -125,7 +126,7 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie override fun prevPageByAnim(animationSpeed: Int) { abortAnim() if (!hasPrev()) return - setDirection(Direction.PREV) + setDirection(PageDirection.PREV) pageView.setTouchPoint(0f, 0f) onAnimStart(animationSpeed) } 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 3a2dabc1d..fba190df5 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 @@ -10,6 +10,7 @@ import com.google.android.material.snackbar.Snackbar import io.legado.app.R import io.legado.app.ui.book.read.page.ContentView import io.legado.app.ui.book.read.page.PageView +import io.legado.app.ui.book.read.page.entities.PageDirection import kotlin.math.abs abstract class PageDelegate(protected val pageView: PageView) { @@ -47,7 +48,7 @@ abstract class PageDelegate(protected val pageView: PageView) { var noNext = true //移动方向 - var mDirection = Direction.NONE + var mDirection = PageDirection.NONE var isCancel = false var isRunning = false var isStarted = false @@ -117,17 +118,17 @@ abstract class PageDelegate(protected val pageView: PageView) { abstract fun prevPageByAnim(animationSpeed: Int) - open fun keyTurnPage(direction: Direction) { + open fun keyTurnPage(direction: PageDirection) { if (isRunning) return when (direction) { - Direction.NEXT -> nextPageByAnim(100) - Direction.PREV -> prevPageByAnim(100) + PageDirection.NEXT -> nextPageByAnim(100) + PageDirection.PREV -> prevPageByAnim(100) else -> return } } @CallSuper - open fun setDirection(direction: Direction) { + open fun setDirection(direction: PageDirection) { mDirection = direction } @@ -149,7 +150,7 @@ abstract class PageDelegate(protected val pageView: PageView) { //取消 isCancel = false //是下一章还是前一章 - setDirection(Direction.NONE) + setDirection(PageDirection.NONE) } /** @@ -184,8 +185,4 @@ abstract class PageDelegate(protected val pageView: PageView) { } - enum class Direction { - NONE, PREV, NEXT - } - } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/SimulationPageDelegate.kt b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/SimulationPageDelegate.kt index 5d0e97ec6..5d58e48d6 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/SimulationPageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/SimulationPageDelegate.kt @@ -6,6 +6,7 @@ import android.os.Build import android.view.MotionEvent import io.legado.app.help.ReadBookConfig import io.legado.app.ui.book.read.page.PageView +import io.legado.app.ui.book.read.page.entities.PageDirection import kotlin.math.* @Suppress("DEPRECATION") @@ -123,13 +124,13 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi } MotionEvent.ACTION_MOVE -> { if ((startY > viewHeight / 3 && startY < viewHeight * 2 / 3) - || mDirection == Direction.PREV + || mDirection == PageDirection.PREV ) { pageView.touchY = viewHeight.toFloat() } if (startY > viewHeight / 3 && startY < viewHeight / 2 - && mDirection == Direction.NEXT + && mDirection == PageDirection.NEXT ) { pageView.touchY = 1f } @@ -137,17 +138,17 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi } } - override fun setDirection(direction: Direction) { + override fun setDirection(direction: PageDirection) { super.setDirection(direction) when (direction) { - Direction.PREV -> + PageDirection.PREV -> //上一页滑动不出现对角 if (startX > viewWidth / 2) { calcCornerXY(startX, viewHeight.toFloat()) } else { calcCornerXY(viewWidth - startX, viewHeight.toFloat()) } - Direction.NEXT -> + PageDirection.NEXT -> if (viewWidth / 2 > startX) { calcCornerXY(viewWidth - startX, startY) } @@ -160,12 +161,12 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi val dy: Float // dy 垂直方向滑动的距离,负值会使滚动向上滚动 if (isCancel) { - dx = if (mCornerX > 0 && mDirection == Direction.NEXT) { + dx = if (mCornerX > 0 && mDirection == PageDirection.NEXT) { (viewWidth - touchX) } else { -touchX } - if (mDirection != Direction.NEXT) { + if (mDirection != PageDirection.NEXT) { dx = -(viewWidth + touchX) } dy = if (mCornerY > 0) { @@ -174,7 +175,7 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi -touchY // 防止mTouchY最终变为0 } } else { - dx = if (mCornerX > 0 && mDirection == Direction.NEXT) { + dx = if (mCornerX > 0 && mDirection == PageDirection.NEXT) { -(viewWidth + touchX) } else { (viewWidth - touchX + viewWidth) @@ -197,14 +198,14 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi override fun onDraw(canvas: Canvas) { if (!isRunning) return when (mDirection) { - Direction.NEXT -> { + PageDirection.NEXT -> { calcPoints() drawCurrentPageArea(canvas, curBitmap) drawNextPageAreaAndShadow(canvas, nextBitmap) drawCurrentPageShadow(canvas) drawCurrentBackArea(canvas, curBitmap) } - Direction.PREV -> { + PageDirection.PREV -> { calcPoints() drawCurrentPageArea(canvas, prevBitmap) drawNextPageAreaAndShadow(canvas, curBitmap) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/SlidePageDelegate.kt b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/SlidePageDelegate.kt index 9c3413f6a..afb7ee499 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/SlidePageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/SlidePageDelegate.kt @@ -3,6 +3,7 @@ package io.legado.app.ui.book.read.page.delegate import android.graphics.Canvas import android.graphics.Matrix import io.legado.app.ui.book.read.page.PageView +import io.legado.app.ui.book.read.page.entities.PageDirection class SlidePageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) { @@ -11,7 +12,7 @@ class SlidePageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) { override fun onAnimStart(animationSpeed: Int) { val distanceX: Float when (mDirection) { - Direction.NEXT -> distanceX = + PageDirection.NEXT -> distanceX = if (isCancel) { var dis = viewWidth - startX + touchX if (dis > viewWidth) { @@ -34,17 +35,17 @@ class SlidePageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) { override fun onDraw(canvas: Canvas) { val offsetX = touchX - startX - if ((mDirection == Direction.NEXT && offsetX > 0) - || (mDirection == Direction.PREV && offsetX < 0) + if ((mDirection == PageDirection.NEXT && offsetX > 0) + || (mDirection == PageDirection.PREV && offsetX < 0) ) return val distanceX = if (offsetX > 0) offsetX - viewWidth else offsetX + viewWidth if (!isRunning) return - if (mDirection == Direction.PREV) { + if (mDirection == PageDirection.PREV) { bitmapMatrix.setTranslate(distanceX + viewWidth, 0.toFloat()) curBitmap?.let { canvas.drawBitmap(it, bitmapMatrix, null) } bitmapMatrix.setTranslate(distanceX, 0.toFloat()) prevBitmap?.let { canvas.drawBitmap(it, bitmapMatrix, null) } - } else if (mDirection == Direction.NEXT) { + } else if (mDirection == PageDirection.NEXT) { bitmapMatrix.setTranslate(distanceX, 0.toFloat()) nextBitmap?.let { canvas.drawBitmap(it, bitmapMatrix, null) } bitmapMatrix.setTranslate(distanceX - viewWidth, 0.toFloat()) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/PageDirection.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/PageDirection.kt new file mode 100644 index 000000000..713c6bd67 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/PageDirection.kt @@ -0,0 +1,5 @@ +package io.legado.app.ui.book.read.page.entities + +enum class PageDirection { + NONE, PREV, NEXT +} \ No newline at end of file