feat: 优化代码

pull/115/head
kunfei 5 years ago
parent af31324bb7
commit f83479ab8a
  1. 11
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  2. 1
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt
  3. 23
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt
  4. 10
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/ScrollPageDelegate.kt

@ -4,6 +4,7 @@ import android.content.Context
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Paint import android.graphics.Paint
import android.util.AttributeSet import android.util.AttributeSet
import android.util.Log
import android.view.View import android.view.View
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
@ -80,10 +81,9 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
private fun drawScrollPage(canvas: Canvas) { private fun drawScrollPage(canvas: Canvas) {
textPage.textLines.forEach { textLine -> textPage.textLines.forEach { textLine ->
val yPy = pageOffset val lineTop = textLine.lineTop + pageOffset
val lineTop = textLine.lineTop + yPy val lineBase = textLine.lineBase + pageOffset
val lineBase = textLine.lineBase + yPy val lineBottom = textLine.lineBottom + pageOffset
val lineBottom = textLine.lineBottom + yPy
drawChars( drawChars(
canvas, canvas,
textLine.textChars, textLine.textChars,
@ -148,7 +148,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
fun onScroll(mOffset: Float) { fun onScroll(mOffset: Float) {
if (mOffset == 0f) return if (mOffset == 0f) return
var offset = -mOffset var offset = mOffset
if (offset > maxScrollOffset) { if (offset > maxScrollOffset) {
offset = maxScrollOffset offset = maxScrollOffset
} else if (offset < -maxScrollOffset) { } else if (offset < -maxScrollOffset) {
@ -159,6 +159,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (pageOffset > 0) { if (pageOffset > 0) {
pageFactory.moveToPrev() pageFactory.moveToPrev()
textPage = pageFactory.currentPage ?: TextPage().format() textPage = pageFactory.currentPage ?: TextPage().format()
Log.e("pageOffSet", pageOffset.toString() + " " + textPage.height)
pageOffset -= textPage.height pageOffset -= textPage.height
callBack.upContent(0, false) callBack.upContent(0, false)
} else if (pageOffset < -textPage.height) { } else if (pageOffset < -textPage.height) {

@ -100,6 +100,7 @@ class ContentView(context: Context) : FrameLayout(context) {
content_text_view.resetPageOffset() content_text_view.resetPageOffset()
content_text_view.setContent(textPage) content_text_view.setContent(textPage)
} }
content_text_view.invalidate()
} }
} }

@ -6,9 +6,9 @@ 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.animation.DecelerateInterpolator
import android.widget.Scroller import android.widget.Scroller
import androidx.annotation.CallSuper import androidx.annotation.CallSuper
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.constant.PreferKey
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
@ -47,16 +47,13 @@ abstract class PageDelegate(protected val pageView: PageView) :
protected var viewWidth: Int = pageView.width protected var viewWidth: Int = pageView.width
protected var viewHeight: Int = pageView.height protected var viewHeight: Int = pageView.height
//textView在顶端或低端
protected var atTop: Boolean = false
protected var atBottom: Boolean = false
private val snackBar: Snackbar by lazy { private val snackBar: Snackbar by lazy {
Snackbar.make(pageView, "", Snackbar.LENGTH_SHORT) Snackbar.make(pageView, "", Snackbar.LENGTH_SHORT)
} }
private val scroller: Scroller by lazy { private val scroller: Scroller by lazy {
Scroller(pageView.context, FastOutLinearInInterpolator()) Scroller(pageView.context, DecelerateInterpolator())
} }
private val detector: GestureDetector by lazy { private val detector: GestureDetector by lazy {
@ -83,7 +80,7 @@ abstract class PageDelegate(protected val pageView: PageView) :
touchY = y touchY = y
if (invalidate) { if (invalidate) {
invalidate() pageView.invalidate()
} }
} }
@ -93,7 +90,7 @@ abstract class PageDelegate(protected val pageView: PageView) :
touchY = y touchY = y
if (invalidate) { if (invalidate) {
invalidate() pageView.postInvalidate()
} }
onScroll() onScroll()
@ -103,10 +100,6 @@ abstract class PageDelegate(protected val pageView: PageView) :
detector.setIsLongpressEnabled(selectAble) detector.setIsLongpressEnabled(selectAble)
} }
protected fun invalidate() {
pageView.invalidate()
}
open fun fling( open fun fling(
startX: Int, startY: Int, velocityX: Int, velocityY: Int, startX: Int, startY: Int, velocityX: Int, velocityY: Int,
minX: Int, maxX: Int, minY: Int, maxY: Int minX: Int, maxX: Int, minY: Int, maxY: Int
@ -114,7 +107,7 @@ abstract class PageDelegate(protected val pageView: PageView) :
scroller.fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY) scroller.fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY)
isRunning = true isRunning = true
isStarted = true isStarted = true
invalidate() pageView.invalidate()
} }
protected fun startScroll(startX: Int, startY: Int, dx: Int, dy: Int) { protected fun startScroll(startX: Int, startY: Int, dx: Int, dy: Int) {
@ -127,13 +120,13 @@ abstract class PageDelegate(protected val pageView: PageView) :
) )
isRunning = true isRunning = true
isStarted = true isStarted = true
invalidate() pageView.invalidate()
} }
private fun stopScroll() { private fun stopScroll() {
isRunning = false isRunning = false
isStarted = false isStarted = false
invalidate() pageView.invalidate()
bitmap?.recycle() bitmap?.recycle()
bitmap = null bitmap = null
} }
@ -141,7 +134,7 @@ abstract class PageDelegate(protected val pageView: PageView) :
fun setViewSize(width: Int, height: Int) { fun setViewSize(width: Int, height: Int) {
viewWidth = width viewWidth = width
viewHeight = height viewHeight = height
invalidate() pageView.invalidate()
centerRectF.set( centerRectF.set(
width * 0.33f, height * 0.33f, width * 0.33f, height * 0.33f,
width * 0.66f, height * 0.66f width * 0.66f, height * 0.66f

@ -22,7 +22,7 @@ class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) {
} }
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
curPage.onScroll(lastY - touchY) curPage.onScroll(touchY - lastY)
} }
override fun onAnimStop() { override fun onAnimStop() {
@ -52,23 +52,19 @@ class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) {
if (!isMoved && abs(distanceX) < abs(distanceY)) { if (!isMoved && abs(distanceX) < abs(distanceY)) {
if (distanceY < 0) { if (distanceY < 0) {
if (atTop) {
//如果上一页不存在 //如果上一页不存在
if (!hasPrev()) { if (!hasPrev()) {
noNext = true noNext = true
return true return true
} }
setDirection(Direction.PREV) setDirection(Direction.PREV)
}
} else { } else {
if (atBottom) { //如果上一页不存在
//如果不存在表示没有下一页了
if (!hasNext()) { if (!hasNext()) {
noNext = true noNext = true
return true return true
} }
setDirection(Direction.NEXT) setDirection(Direction.PREV)
}
} }
isMoved = true isMoved = true
} }

Loading…
Cancel
Save