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. 52
      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,10 +215,8 @@ 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()
//是否移动 //是否移动
@ -244,6 +234,9 @@ abstract class PageDelegate(protected val pageView: PageView) {
return true return true
} }
/**
* 单击
*/
override fun onSingleTapUp(e: MotionEvent): Boolean { override fun onSingleTapUp(e: MotionEvent): Boolean {
val x = e.x val x = e.x
val y = e.y val y = e.y
@ -273,15 +266,9 @@ abstract class PageDelegate(protected val pageView: PageView) {
return true return true
} }
override fun onScroll( /**
e1: MotionEvent, * 移动结束
e2: MotionEvent, */
distanceX: Float,
distanceY: Float
): Boolean {
return this@PageDelegate.onScroll(e1, e2, distanceX, distanceY)
}
override fun onFling( override fun onFling(
e1: MotionEvent?, e1: MotionEvent?,
e2: MotionEvent?, e2: MotionEvent?,
@ -291,10 +278,11 @@ abstract class PageDelegate(protected val pageView: PageView) {
if (!noNext) onAnimStart() if (!noNext) onAnimStart()
return true 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