优化翻页

pull/357/head
gedoor 4 years ago
parent 9595eb8990
commit c0eb17cb31
  1. 2
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt
  2. 17
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt
  3. 10
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/NoAnimPageDelegate.kt
  4. 18
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt
  5. 25
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/ScrollPageDelegate.kt

@ -200,11 +200,9 @@ class PageView(context: Context, attrs: AttributeSet) :
lastY = touchY
touchX = x
touchY = y
if (invalidate) {
invalidate()
}
pageDelegate?.onScroll()
}

@ -37,10 +37,7 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
override fun onTouch(event: MotionEvent) {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
if (abort()) {
onAnimStop()
stopScroll()
}
abortAnim()
}
MotionEvent.ACTION_MOVE -> {
onScroll(event)
@ -101,8 +98,16 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
}
}
override fun abortAnim() {
if (!scroller.isFinished) {
scroller.abortAnimation()
onAnimStop()
stopScroll()
}
}
override fun nextPageByAnim(animationSpeed: Int) {
abort()
abortAnim()
if (!hasNext()) return
setDirection(Direction.NEXT)
pageView.setTouchPoint(viewWidth.toFloat(), 0f)
@ -110,7 +115,7 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
}
override fun prevPageByAnim(animationSpeed: Int) {
abort()
abortAnim()
if (!hasPrev()) return
setDirection(Direction.PREV)
pageView.setTouchPoint(0f, 0f)

@ -1,5 +1,6 @@
package io.legado.app.ui.book.read.page.delegate
import android.graphics.Canvas
import io.legado.app.ui.book.read.page.PageView
class NoAnimPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
@ -11,4 +12,13 @@ class NoAnimPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView)
stopScroll()
}
override fun onDraw(canvas: Canvas) {
// nothing
}
override fun onAnimStop() {
// nothing
}
}

@ -35,7 +35,7 @@ abstract class PageDelegate(protected val pageView: PageView) {
protected var viewWidth: Int = pageView.width
protected var viewHeight: Int = pageView.height
private val scroller: Scroller by lazy {
protected val scroller: Scroller by lazy {
Scroller(pageView.context, DecelerateInterpolator())
}
@ -103,21 +103,15 @@ abstract class PageDelegate(protected val pageView: PageView) {
}
}
fun abort(): Boolean {
if (!scroller.isFinished) {
scroller.abortAnimation()
return true
}
return false
}
open fun onScroll() = Unit
open fun onAnimStart(animationSpeed: Int) {}//scroller start
abstract fun abortAnim()
open fun onDraw(canvas: Canvas) {}//绘制
abstract fun onAnimStart(animationSpeed: Int) //scroller start
open fun onAnimStop() {}//scroller finish
abstract fun onDraw(canvas: Canvas) //绘制
open fun onScroll() {}//移动contentView, slidePage
abstract fun onAnimStop() //scroller finish
abstract fun nextPageByAnim(animationSpeed: Int)

@ -1,5 +1,6 @@
package io.legado.app.ui.book.read.page.delegate
import android.graphics.Canvas
import android.view.MotionEvent
import android.view.VelocityTracker
import io.legado.app.ui.book.read.page.PageView
@ -20,15 +21,15 @@ class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) {
)
}
override fun onScroll() {
curPage.onScroll(touchY - lastY)
override fun onAnimStop() {
// nothing
}
override fun onTouch(event: MotionEvent) {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
abortAnim()
pageView.setStartPoint(event.x, event.y)
abort()
mVelocity.clear()
}
MotionEvent.ACTION_MOVE -> {
@ -40,6 +41,14 @@ class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) {
}
}
override fun onScroll() {
curPage.onScroll(touchY - lastY)
}
override fun onDraw(canvas: Canvas) {
// nothing
}
private fun onScroll(event: MotionEvent) {
mVelocity.addMovement(event)
mVelocity.computeCurrentVelocity(velocityDuration)
@ -76,14 +85,20 @@ class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) {
mVelocity.recycle()
}
override fun abortAnim() {
if (!scroller.isFinished) {
scroller.abortAnimation()
}
}
override fun nextPageByAnim(animationSpeed: Int) {
abort()
abortAnim()
pageView.setStartPoint(0f, 0f, false)
startScroll(0, 0, 0, -ChapterProvider.visibleHeight, animationSpeed)
}
override fun prevPageByAnim(animationSpeed: Int) {
abort()
abortAnim()
pageView.setStartPoint(0f, 0f, false)
startScroll(0, 0, 0, ChapterProvider.visibleHeight, animationSpeed)
}

Loading…
Cancel
Save