feat: 优化代码

pull/106/head
kunfei 5 years ago
parent d4d76e8722
commit 4c34c6e7f2
  1. 17
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  2. 146
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt

@ -2,11 +2,14 @@ package io.legado.app.ui.book.read.page
import android.content.Context import android.content.Context
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet import android.util.AttributeSet
import android.view.View import android.view.View
import io.legado.app.R
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.accentColor
import io.legado.app.ui.book.read.page.entities.TextPage import io.legado.app.ui.book.read.page.entities.TextPage
import io.legado.app.utils.getCompatColor
class ContentTextView : View { class ContentTextView : View {
@ -17,6 +20,11 @@ class ContentTextView : View {
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int)
: super(context, attrs, defStyleAttr) : super(context, attrs, defStyleAttr)
private val selectedPaint by lazy {
Paint().apply {
color = context.getCompatColor(R.color.btn_bg_press)
}
}
var textPage: TextPage? = null var textPage: TextPage? = null
fun setContent(textPage: TextPage?) { fun setContent(textPage: TextPage?) {
@ -54,6 +62,15 @@ class ContentTextView : View {
it.leftBottomPosition.y.toFloat(), it.leftBottomPosition.y.toFloat(),
textPaint textPaint
) )
if (it.selected) {
canvas.drawRect(
it.leftBottomPosition.x,
it.rightTopPosition.y.toFloat(),
it.rightTopPosition.x,
it.leftBottomPosition.y.toFloat(),
selectedPaint
)
}
} }
} }
} }

@ -10,13 +10,16 @@ import android.widget.Scroller
import androidx.annotation.CallSuper import androidx.annotation.CallSuper
import androidx.interpolator.view.animation.FastOutLinearInInterpolator import androidx.interpolator.view.animation.FastOutLinearInInterpolator
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
abstract class PageDelegate(protected val pageView: PageView) { abstract class PageDelegate(protected val pageView: PageView) :
GestureDetector.SimpleOnGestureListener() {
val centerRectF = RectF( val centerRectF = RectF(
pageView.width * 0.33f, pageView.height * 0.33f, pageView.width * 0.33f, pageView.height * 0.33f,
pageView.width * 0.66f, pageView.height * 0.66f 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 { private val scroller: Scroller by lazy {
Scroller( Scroller(pageView.context, FastOutLinearInInterpolator())
pageView.context,
FastOutLinearInInterpolator()
)
} }
private val detector: GestureDetector by lazy { private val detector: GestureDetector by lazy {
GestureDetector( GestureDetector(pageView.context, this).apply {
pageView.context, setIsLongpressEnabled(context.getPrefBoolean(PreferKey.selectText))
GestureListener() }
)
} }
var isMoved = false var isMoved = false
@ -188,13 +187,6 @@ abstract class PageDelegate(protected val pageView: PageView) {
open fun onScroll() {//移动contentView, slidePage open fun onScroll() {//移动contentView, slidePage
} }
abstract fun onScroll(
e1: MotionEvent,
e2: MotionEvent,
distanceX: Float,
distanceY: Float
): Boolean
enum class Direction { enum class Direction {
NONE, PREV, NEXT 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() // abort()
//是否移动 //是否移动
isMoved = false isMoved = false
//是否存在下一章 //是否存在下一章
noNext = false noNext = false
//是否正在执行动画 //是否正在执行动画
isRunning = false isRunning = false
//取消 //取消
isCancel = false isCancel = false
//是下一章还是前一章 //是下一章还是前一章
setDirection(Direction.NONE) setDirection(Direction.NONE)
//设置起始位置的触摸点 //设置起始位置的触摸点
setStartPoint(e.x, e.y) setStartPoint(e.x, e.y)
return true return true
} }
override fun onSingleTapUp(e: MotionEvent): Boolean { /**
val x = e.x * 单击
val y = e.y */
if (centerRectF.contains(x, y)) { override fun onSingleTapUp(e: MotionEvent): Boolean {
pageView.callBack?.clickCenter() val x = e.x
setTouchPoint(x, y) 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 { } else {
if (x > viewWidth / 2 || if (!hasPrev()) {
AppConfig.clickAllNext return true
) {
//设置动画方向
if (!hasNext()) {
return true
}
setDirection(Direction.NEXT)
setBitmap()
} else {
if (!hasPrev()) {
return true
}
setDirection(Direction.PREV)
setBitmap()
} }
setTouchPoint(x, y) setDirection(Direction.PREV)
onAnimStart() setBitmap()
} }
return true setTouchPoint(x, y)
} onAnimStart()
override fun onScroll(
e1: MotionEvent,
e2: MotionEvent,
distanceX: Float,
distanceY: Float
): Boolean {
return this@PageDelegate.onScroll(e1, e2, distanceX, distanceY)
} }
return true
}
override fun onFling( /**
e1: MotionEvent?, * 移动结束
e2: MotionEvent?, */
velocityX: Float, override fun onFling(
velocityY: Float e1: MotionEvent?,
): Boolean { e2: MotionEvent?,
if (!noNext) onAnimStart() velocityX: Float,
return true velocityY: Float
} ): Boolean {
if (!noNext) onAnimStart()
return true
} }
/**
* 判断是否有上一页
*/
fun hasPrev(): Boolean { fun hasPrev(): Boolean {
//上一页的参数配置
val hasPrev = pageView.pageFactory?.hasPrev() == true val hasPrev = pageView.pageFactory?.hasPrev() == true
if (!hasPrev) { if (!hasPrev) {
if (!snackBar.isShown) { if (!snackBar.isShown) {
@ -305,8 +293,10 @@ abstract class PageDelegate(protected val pageView: PageView) {
return hasPrev return hasPrev
} }
/**
* 判断是否有下一页
*/
fun hasNext(): Boolean { fun hasNext(): Boolean {
//进行下一页的配置
val hasNext = pageView.pageFactory?.hasNext() == true val hasNext = pageView.pageFactory?.hasNext() == true
if (!hasNext) { if (!hasNext) {
if (!snackBar.isShown) { if (!snackBar.isShown) {

Loading…
Cancel
Save