From 4c34c6e7f2985cb3d7c7231bcb6f996a12106689 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 21 Feb 2020 14:13:01 +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 --- .../app/ui/book/read/page/ContentTextView.kt | 17 ++ .../book/read/page/delegate/PageDelegate.kt | 146 ++++++++---------- 2 files changed, 85 insertions(+), 78 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt index 72d8fece6..2c1cbad80 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt @@ -2,11 +2,14 @@ package io.legado.app.ui.book.read.page import android.content.Context import android.graphics.Canvas +import android.graphics.Paint import android.util.AttributeSet import android.view.View +import io.legado.app.R import io.legado.app.help.ReadBookConfig import io.legado.app.lib.theme.accentColor import io.legado.app.ui.book.read.page.entities.TextPage +import io.legado.app.utils.getCompatColor class ContentTextView : View { @@ -17,6 +20,11 @@ class ContentTextView : View { constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) + private val selectedPaint by lazy { + Paint().apply { + color = context.getCompatColor(R.color.btn_bg_press) + } + } var textPage: TextPage? = null fun setContent(textPage: TextPage?) { @@ -54,6 +62,15 @@ class ContentTextView : View { it.leftBottomPosition.y.toFloat(), textPaint ) + if (it.selected) { + canvas.drawRect( + it.leftBottomPosition.x, + it.rightTopPosition.y.toFloat(), + it.rightTopPosition.x, + it.leftBottomPosition.y.toFloat(), + selectedPaint + ) + } } } } 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 ac0bb9aff..d8a03a8cb 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,13 +10,16 @@ import android.widget.Scroller import androidx.annotation.CallSuper import androidx.interpolator.view.animation.FastOutLinearInInterpolator import com.google.android.material.snackbar.Snackbar +import io.legado.app.constant.PreferKey import io.legado.app.help.AppConfig import io.legado.app.ui.book.read.page.ContentView import io.legado.app.ui.book.read.page.PageView +import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.screenshot import kotlin.math.abs -abstract class PageDelegate(protected val pageView: PageView) { +abstract class PageDelegate(protected val pageView: PageView) : + GestureDetector.SimpleOnGestureListener() { val centerRectF = RectF( pageView.width * 0.33f, pageView.height * 0.33f, pageView.width * 0.66f, pageView.height * 0.66f @@ -51,17 +54,13 @@ abstract class PageDelegate(protected val pageView: PageView) { } private val scroller: Scroller by lazy { - Scroller( - pageView.context, - FastOutLinearInInterpolator() - ) + Scroller(pageView.context, FastOutLinearInInterpolator()) } private val detector: GestureDetector by lazy { - GestureDetector( - pageView.context, - GestureListener() - ) + GestureDetector(pageView.context, this).apply { + setIsLongpressEnabled(context.getPrefBoolean(PreferKey.selectText)) + } } var isMoved = false @@ -188,13 +187,6 @@ abstract class PageDelegate(protected val pageView: PageView) { open fun onScroll() {//移动contentView, slidePage } - abstract fun onScroll( - e1: MotionEvent, - e2: MotionEvent, - distanceX: Float, - distanceY: Float - ): Boolean - enum class Direction { NONE, PREV, NEXT } @@ -223,78 +215,74 @@ abstract class PageDelegate(protected val pageView: PageView) { } /** - * 触摸事件处理 + * 按下 */ - private inner class GestureListener : GestureDetector.SimpleOnGestureListener() { - - override fun onDown(e: MotionEvent): Boolean { + override fun onDown(e: MotionEvent): Boolean { // abort() - //是否移动 - isMoved = false - //是否存在下一章 - noNext = false - //是否正在执行动画 - isRunning = false - //取消 - isCancel = false - //是下一章还是前一章 - setDirection(Direction.NONE) - //设置起始位置的触摸点 - setStartPoint(e.x, e.y) - return true - } + //是否移动 + isMoved = false + //是否存在下一章 + noNext = false + //是否正在执行动画 + isRunning = false + //取消 + isCancel = false + //是下一章还是前一章 + setDirection(Direction.NONE) + //设置起始位置的触摸点 + setStartPoint(e.x, e.y) + return true + } - override fun onSingleTapUp(e: MotionEvent): Boolean { - val x = e.x - val y = e.y - if (centerRectF.contains(x, y)) { - pageView.callBack?.clickCenter() - setTouchPoint(x, y) + /** + * 单击 + */ + override fun onSingleTapUp(e: MotionEvent): Boolean { + val x = e.x + val y = e.y + if (centerRectF.contains(x, y)) { + pageView.callBack?.clickCenter() + setTouchPoint(x, y) + } else { + if (x > viewWidth / 2 || + AppConfig.clickAllNext + ) { + //设置动画方向 + if (!hasNext()) { + return true + } + setDirection(Direction.NEXT) + setBitmap() } else { - if (x > viewWidth / 2 || - AppConfig.clickAllNext - ) { - //设置动画方向 - if (!hasNext()) { - return true - } - setDirection(Direction.NEXT) - setBitmap() - } else { - if (!hasPrev()) { - return true - } - setDirection(Direction.PREV) - setBitmap() + if (!hasPrev()) { + return true } - setTouchPoint(x, y) - onAnimStart() + setDirection(Direction.PREV) + setBitmap() } - return true - } - - override fun onScroll( - e1: MotionEvent, - e2: MotionEvent, - distanceX: Float, - distanceY: Float - ): Boolean { - return this@PageDelegate.onScroll(e1, e2, distanceX, distanceY) + setTouchPoint(x, y) + onAnimStart() } + return true + } - override fun onFling( - e1: MotionEvent?, - e2: MotionEvent?, - velocityX: Float, - velocityY: Float - ): Boolean { - if (!noNext) onAnimStart() - return true - } + /** + * 移动结束 + */ + override fun onFling( + e1: MotionEvent?, + e2: MotionEvent?, + velocityX: Float, + velocityY: Float + ): Boolean { + if (!noNext) onAnimStart() + return true } + /** + * 判断是否有上一页 + */ fun hasPrev(): Boolean { - //上一页的参数配置 val hasPrev = pageView.pageFactory?.hasPrev() == true if (!hasPrev) { if (!snackBar.isShown) { @@ -305,8 +293,10 @@ abstract class PageDelegate(protected val pageView: PageView) { return hasPrev } + /** + * 判断是否有下一页 + */ fun hasNext(): Boolean { - //进行下一页的配置 val hasNext = pageView.pageFactory?.hasNext() == true if (!hasNext) { if (!snackBar.isShown) {