feat: 优化代码

pull/117/head
kunfei 5 years ago
parent 3424d7d8f4
commit 57c9216bcf
  1. 62
      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,41 +6,41 @@ 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) {
if (abs(distanceX) > abs(distanceY)) {
if (distanceX < 0) {
//如果上一页不存在
if (!hasPrev()) {
noNext = true
return true
override fun onTouch(event: MotionEvent) {
when (event.action) {
MotionEvent.ACTION_MOVE -> {
//判断是否移动了
if (!isMoved) {
isMoved = abs(startX - event.x) > slop || abs(startY - event.y) > slop
if (isMoved) {
if (event.x - startX > 0) {
//如果上一页不存在
if (!hasPrev()) {
noNext = true
return
}
setDirection(Direction.PREV)
setBitmap()
} else {
//如果不存在表示没有下一页了
if (!hasNext()) {
noNext = true
return
}
setDirection(Direction.NEXT)
setBitmap()
}
}
setDirection(Direction.PREV)
setBitmap()
} else {
//如果不存在表示没有下一页了
if (!hasNext()) {
noNext = true
return true
}
setDirection(Direction.NEXT)
setBitmap()
}
isMoved = true
if (isMoved) {
isCancel = if (mDirection == Direction.NEXT) touchX > lastX else touchX < lastX
isRunning = true
//设置触摸点
setTouchPoint(event.x, event.y)
}
}
}
if (isMoved) {
isCancel = if (mDirection == Direction.NEXT) distanceX < 0 else distanceX > 0
isRunning = true
//设置触摸点
setTouchPoint(e2.x, e2.y)
}
return isMoved
super.onTouch(event)
}
}

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

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

Loading…
Cancel
Save