feat: 优化代码

pull/117/head
kunfei 5 years ago
parent 9ea581aabf
commit ed1391b3bb
  1. 17
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt
  2. 22
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt
  3. 24
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/ScrollPageDelegate.kt

@ -12,14 +12,14 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
if (isTextSelected) {
selectText(event)
} else {
scroll(event)
onScroll(event)
}
}
}
super.onTouch(event)
}
private fun scroll(event: MotionEvent) {
private fun onScroll(event: MotionEvent) {
//判断是否移动了
if (!isMoved) {
isMoved = abs(startX - event.x) > slop || abs(startY - event.y) > slop
@ -51,17 +51,4 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
}
}
private fun selectText(event: MotionEvent) {
curPage.selectText(event) { lineIndex, charIndex ->
if (lineIndex > firstLineIndex
|| (lineIndex == firstLineIndex && charIndex > firstCharIndex)
) {
curPage.selectStartMoveIndex(firstLineIndex, firstCharIndex)
curPage.selectEndMoveIndex(lineIndex, charIndex)
} else {
curPage.selectEndMoveIndex(firstLineIndex, firstCharIndex)
curPage.selectStartMoveIndex(lineIndex, charIndex)
}
}
}
}

@ -264,15 +264,11 @@ abstract class PageDelegate(protected val pageView: PageView) :
AppConfig.clickAllNext
) {
//设置动画方向
if (!hasNext()) {
return true
}
if (!hasNext()) return true
setDirection(Direction.NEXT)
setBitmap()
} else {
if (!hasPrev()) {
return true
}
if (!hasPrev()) return true
setDirection(Direction.PREV)
setBitmap()
}
@ -293,6 +289,20 @@ abstract class PageDelegate(protected val pageView: PageView) :
}
}
protected fun selectText(event: MotionEvent) {
curPage.selectText(event) { lineIndex, charIndex ->
if (lineIndex > firstLineIndex
|| (lineIndex == firstLineIndex && charIndex > firstCharIndex)
) {
curPage.selectStartMoveIndex(firstLineIndex, firstCharIndex)
curPage.selectEndMoveIndex(lineIndex, charIndex)
} else {
curPage.selectEndMoveIndex(firstLineIndex, firstCharIndex)
curPage.selectStartMoveIndex(lineIndex, charIndex)
}
}
}
/**
* 判断是否有上一页
*/

@ -33,20 +33,28 @@ class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) {
mVelocity.clear()
}
MotionEvent.ACTION_MOVE -> {
mVelocity.addMovement(event)
mVelocity.computeCurrentVelocity(velocityDuration)
setTouchPoint(event.x, event.y)
if (!isMoved) {
isMoved = abs(startX - event.x) > slop || abs(startY - event.y) > slop
}
if (isMoved) {
isRunning = true
if (isTextSelected) {
selectText(event)
} else {
onScroll(event)
}
}
}
super.onTouch(event)
}
private fun onScroll(event: MotionEvent) {
mVelocity.addMovement(event)
mVelocity.computeCurrentVelocity(velocityDuration)
setTouchPoint(event.x, event.y)
if (!isMoved) {
isMoved = abs(startX - event.x) > slop || abs(startY - event.y) > slop
}
if (isMoved) {
isRunning = true
}
}
override fun onDestroy() {
super.onDestroy()
mVelocity.recycle()

Loading…
Cancel
Save