feat: 优化代码

pull/117/head
kunfei 5 years ago
parent 3424d7d8f4
commit 57c9216bcf
  1. 28
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt
  2. 11
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt
  3. 4
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/ScrollPageDelegate.kt

@ -6,19 +6,18 @@ import kotlin.math.abs
abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageView) { abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageView) {
override fun onScroll( override fun onTouch(event: MotionEvent) {
e1: MotionEvent, when (event.action) {
e2: MotionEvent, MotionEvent.ACTION_MOVE -> {
distanceX: Float, //判断是否移动了
distanceY: Float
): Boolean {
if (!isMoved) { if (!isMoved) {
if (abs(distanceX) > abs(distanceY)) { isMoved = abs(startX - event.x) > slop || abs(startY - event.y) > slop
if (distanceX < 0) { if (isMoved) {
if (event.x - startX > 0) {
//如果上一页不存在 //如果上一页不存在
if (!hasPrev()) { if (!hasPrev()) {
noNext = true noNext = true
return true return
} }
setDirection(Direction.PREV) setDirection(Direction.PREV)
setBitmap() setBitmap()
@ -26,21 +25,22 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
//如果不存在表示没有下一页了 //如果不存在表示没有下一页了
if (!hasNext()) { if (!hasNext()) {
noNext = true noNext = true
return true return
} }
setDirection(Direction.NEXT) setDirection(Direction.NEXT)
setBitmap() setBitmap()
} }
isMoved = true
} }
} }
if (isMoved) { if (isMoved) {
isCancel = if (mDirection == Direction.NEXT) distanceX < 0 else distanceX > 0 isCancel = if (mDirection == Direction.NEXT) touchX > lastX else touchX < lastX
isRunning = true isRunning = true
//设置触摸点 //设置触摸点
setTouchPoint(e2.x, e2.y) setTouchPoint(event.x, event.y)
}
}
} }
return isMoved super.onTouch(event)
} }
} }

@ -6,15 +6,14 @@ import android.graphics.Canvas
import android.graphics.RectF import android.graphics.RectF
import android.view.GestureDetector import android.view.GestureDetector
import android.view.MotionEvent import android.view.MotionEvent
import android.view.ViewConfiguration
import android.view.animation.DecelerateInterpolator import android.view.animation.DecelerateInterpolator
import android.widget.Scroller import android.widget.Scroller
import androidx.annotation.CallSuper import androidx.annotation.CallSuper
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import io.legado.app.constant.PreferKey
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.ui.book.read.page.ContentView 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.PageView
import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.screenshot import io.legado.app.utils.screenshot
import kotlin.math.abs import kotlin.math.abs
@ -25,10 +24,12 @@ abstract class PageDelegate(protected val pageView: PageView) :
pageView.width * 0.66f, pageView.height * 0.66f pageView.width * 0.66f, pageView.height * 0.66f
) )
protected val context: Context = pageView.context protected val context: Context = pageView.context
protected val slop = ViewConfiguration.get(context).scaledTouchSlop
//起始点 //起始点
protected var startX: Float = 0f protected var startX: Float = 0f
protected var startY: Float = 0f protected var startY: Float = 0f
//上一个触碰点 //上一个触碰点
protected var lastX: Float = 0f
protected var lastY: Float = 0f protected var lastY: Float = 0f
//触碰点 //触碰点
protected var touchX: Float = 0f protected var touchX: Float = 0f
@ -52,9 +53,7 @@ abstract class PageDelegate(protected val pageView: PageView) :
} }
private val detector: GestureDetector by lazy { private val detector: GestureDetector by lazy {
GestureDetector(pageView.context, this).apply { GestureDetector(pageView.context, this)
setIsLongpressEnabled(context.getPrefBoolean(PreferKey.textSelectAble))
}
} }
var isMoved = false var isMoved = false
@ -70,6 +69,7 @@ abstract class PageDelegate(protected val pageView: PageView) :
open fun setStartPoint(x: Float, y: Float, invalidate: Boolean = true) { open fun setStartPoint(x: Float, y: Float, invalidate: Boolean = true) {
startX = x startX = x
startY = y startY = y
lastX = x
lastY = y lastY = y
touchX = x touchX = x
touchY = y touchY = y
@ -80,6 +80,7 @@ abstract class PageDelegate(protected val pageView: PageView) :
} }
open fun setTouchPoint(x: Float, y: Float, invalidate: Boolean = true) { open fun setTouchPoint(x: Float, y: Float, invalidate: Boolean = true) {
lastX = touchX
lastY = touchY lastY = touchY
touchX = x touchX = x
touchY = y touchY = y

@ -2,7 +2,6 @@ package io.legado.app.ui.book.read.page.delegate
import android.view.MotionEvent import android.view.MotionEvent
import android.view.VelocityTracker import android.view.VelocityTracker
import android.view.ViewConfiguration
import io.legado.app.ui.book.read.page.PageView import io.legado.app.ui.book.read.page.PageView
import kotlin.math.abs import kotlin.math.abs
@ -12,7 +11,6 @@ class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) {
private val velocityDuration = 1000 private val velocityDuration = 1000
//速度追踪器 //速度追踪器
private val mVelocity: VelocityTracker = VelocityTracker.obtain() private val mVelocity: VelocityTracker = VelocityTracker.obtain()
private val slop = ViewConfiguration.get(pageView.context).scaledTouchSlop
override fun onAnimStart() { override fun onAnimStart() {
//惯性滚动 //惯性滚动
@ -39,7 +37,7 @@ class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) {
mVelocity.computeCurrentVelocity(velocityDuration) mVelocity.computeCurrentVelocity(velocityDuration)
setTouchPoint(event.x, event.y) setTouchPoint(event.x, event.y)
if (!isMoved) { if (!isMoved) {
isMoved = abs(startY - event.y) > slop isMoved = abs(startX - event.x) > slop || abs(startY - event.y) > slop
} }
if (isMoved) { if (isMoved) {
isRunning = true isRunning = true

Loading…
Cancel
Save