feat: 优化代码

pull/103/head
kunfei 5 years ago
parent 4690b2253e
commit 3d2a0fa002
  1. 12
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/CoverPageDelegate.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt
  3. 2
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/NoAnimPageDelegate.kt
  4. 12
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt
  5. 16
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/ScrollPageDelegate.kt
  6. 32
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/SimulationPageDelegate.kt
  7. 12
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/SlidePageDelegate.kt

@ -20,7 +20,7 @@ class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
override fun onScrollStart() { override fun onScrollStart() {
val distanceX: Float val distanceX: Float
when (direction) { when (mDirection) {
Direction.NEXT -> distanceX = Direction.NEXT -> distanceX =
if (isCancel) { if (isCancel) {
var dis = viewWidth - startX + touchX var dis = viewWidth - startX + touchX
@ -45,23 +45,23 @@ class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
override fun onScrollStop() { override fun onScrollStop() {
curPage.x = 0.toFloat() curPage.x = 0.toFloat()
if (!isCancel) { if (!isCancel) {
pageView.fillPage(direction) pageView.fillPage(mDirection)
} }
} }
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
val offsetX = touchX - startX val offsetX = touchX - startX
if ((direction == Direction.NEXT && offsetX > 0) if ((mDirection == Direction.NEXT && offsetX > 0)
|| (direction == Direction.PREV && offsetX < 0) || (mDirection == Direction.PREV && offsetX < 0)
) return ) return
val distanceX = if (offsetX > 0) offsetX - viewWidth else offsetX + viewWidth val distanceX = if (offsetX > 0) offsetX - viewWidth else offsetX + viewWidth
bitmap?.let { bitmap?.let {
if (direction == Direction.PREV) { if (mDirection == Direction.PREV) {
bitmapMatrix.setTranslate(distanceX, 0.toFloat()) bitmapMatrix.setTranslate(distanceX, 0.toFloat())
canvas.drawBitmap(it, bitmapMatrix, null) canvas.drawBitmap(it, bitmapMatrix, null)
} else if (direction == Direction.NEXT) { } else if (mDirection == Direction.NEXT) {
curPage.translationX = offsetX curPage.translationX = offsetX
} }
addShadow(distanceX.toInt(), canvas) addShadow(distanceX.toInt(), canvas)

@ -41,7 +41,7 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
} }
} }
if (isMoved) { if (isMoved) {
isCancel = if (direction == Direction.NEXT) distanceX < 0 else distanceX > 0 isCancel = if (mDirection == Direction.NEXT) distanceX < 0 else distanceX > 0
isRunning = true isRunning = true
//设置触摸点 //设置触摸点
setTouchPoint(e2.x, e2.y) setTouchPoint(e2.x, e2.y)

@ -13,7 +13,7 @@ class NoAnimPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView)
override fun onScrollStop() { override fun onScrollStop() {
if (!isCancel) { if (!isCancel) {
pageView.fillPage(direction) pageView.fillPage(mDirection)
} }
} }
} }

@ -67,7 +67,7 @@ abstract class PageDelegate(protected val pageView: PageView) {
var noNext = true var noNext = true
//移动方向 //移动方向
var direction = Direction.NONE var mDirection = Direction.NONE
var isCancel = false var isCancel = false
var isRunning = false var isRunning = false
var isStarted = false var isStarted = false
@ -229,6 +229,10 @@ abstract class PageDelegate(protected val pageView: PageView) {
NONE, PREV, NEXT NONE, PREV, NEXT
} }
open fun setDirection(direction: Direction) {
mDirection = direction
}
/** /**
* 触摸事件处理 * 触摸事件处理
*/ */
@ -245,7 +249,7 @@ abstract class PageDelegate(protected val pageView: PageView) {
//取消 //取消
isCancel = false isCancel = false
//是下一章还是前一章 //是下一章还是前一章
direction = Direction.NONE setDirection(Direction.NONE)
//设置起始位置的触摸点 //设置起始位置的触摸点
setStartPoint(e.x, e.y) setStartPoint(e.x, e.y)
return true return true
@ -292,7 +296,7 @@ abstract class PageDelegate(protected val pageView: PageView) {
fun hasPrev(): Boolean { fun hasPrev(): Boolean {
//上一页的参数配置 //上一页的参数配置
direction = Direction.PREV setDirection(Direction.PREV)
val hasPrev = pageView.pageFactory?.hasPrev() == true val hasPrev = pageView.pageFactory?.hasPrev() == true
if (!hasPrev) { if (!hasPrev) {
snackBar ?: let { snackBar ?: let {
@ -310,7 +314,7 @@ abstract class PageDelegate(protected val pageView: PageView) {
fun hasNext(): Boolean { fun hasNext(): Boolean {
//进行下一页的配置 //进行下一页的配置
direction = Direction.NEXT setDirection(Direction.NEXT)
val hasNext = pageView.pageFactory?.hasNext() == true val hasNext = pageView.pageFactory?.hasNext() == true
if (!hasNext) { if (!hasNext) {
snackBar ?: let { snackBar ?: let {

@ -20,7 +20,7 @@ class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) {
return return
} }
val distanceY: Float val distanceY: Float
when (direction) { when (mDirection) {
Direction.NEXT -> distanceY = Direction.NEXT -> distanceY =
if (isCancel) { if (isCancel) {
var dis = viewHeight - startY + touchY var dis = viewHeight - startY + touchY
@ -46,17 +46,17 @@ class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) {
if (atTop || atBottom) { if (atTop || atBottom) {
val offsetY = touchY - startY val offsetY = touchY - startY
if ((direction == Direction.NEXT && offsetY > 0) if ((mDirection == Direction.NEXT && offsetY > 0)
|| (direction == Direction.PREV && offsetY < 0) || (mDirection == Direction.PREV && offsetY < 0)
) return ) return
val distanceY = if (offsetY > 0) offsetY - viewHeight else offsetY + viewHeight val distanceY = if (offsetY > 0) offsetY - viewHeight else offsetY + viewHeight
if (atTop && direction == Direction.PREV) { if (atTop && mDirection == Direction.PREV) {
bitmap?.let { bitmap?.let {
bitmapMatrix.setTranslate(0.toFloat(), distanceY) bitmapMatrix.setTranslate(0.toFloat(), distanceY)
canvas.drawBitmap(it, bitmapMatrix, null) canvas.drawBitmap(it, bitmapMatrix, null)
} }
} else if (atBottom && direction == Direction.NEXT) { } else if (atBottom && mDirection == Direction.NEXT) {
bitmap?.let { bitmap?.let {
bitmapMatrix.setTranslate(0.toFloat(), distanceY) bitmapMatrix.setTranslate(0.toFloat(), distanceY)
canvas.drawBitmap(it, bitmapMatrix, null) canvas.drawBitmap(it, bitmapMatrix, null)
@ -67,7 +67,7 @@ class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) {
override fun onScrollStop() { override fun onScrollStop() {
if (!isCancel) { if (!isCancel) {
pageView.fillPage(direction) pageView.fillPage(mDirection)
} }
} }
@ -107,12 +107,12 @@ class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) {
} }
isMoved = true isMoved = true
} }
if ((atTop && direction != Direction.PREV) || (atBottom && direction != Direction.NEXT) || direction == Direction.NONE) { if ((atTop && mDirection != Direction.PREV) || (atBottom && mDirection != Direction.NEXT) || mDirection == Direction.NONE) {
//传递触摸事件到textView //传递触摸事件到textView
curPage.dispatchTouchEvent(e2) curPage.dispatchTouchEvent(e2)
} }
if (isMoved) { if (isMoved) {
isCancel = if (direction == Direction.NEXT) distanceY < 0 else distanceY > 0 isCancel = if (mDirection == Direction.NEXT) distanceY < 0 else distanceY > 0
isRunning = true isRunning = true
//设置触摸点 //设置触摸点
setTouchPoint(e2.x, e2.y) setTouchPoint(e2.x, e2.y)

@ -92,14 +92,14 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
//触摸y中间位置吧y变成屏幕高度 //触摸y中间位置吧y变成屏幕高度
if ((startY > pageView.height / 3.0 if ((startY > pageView.height / 3.0
&& startY < pageView.height * 2 / 3.0) && startY < pageView.height * 2 / 3.0)
|| direction == Direction.PREV || mDirection == Direction.PREV
) { ) {
touchY = pageView.height.toFloat() touchY = pageView.height.toFloat()
} }
if (startY > pageView.height / 3.0 if (startY > pageView.height / 3.0
&& startY < pageView.height / 2.0 && startY < pageView.height / 2.0
&& direction == Direction.NEXT && mDirection == Direction.NEXT
) { ) {
touchY = 1f touchY = 1f
} }
@ -139,7 +139,7 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
} }
} }
if (isMoved) { if (isMoved) {
isCancel = if (direction == Direction.NEXT) distanceX < 0 else distanceX > 0 isCancel = if (mDirection == Direction.NEXT) distanceX < 0 else distanceX > 0
isRunning = true isRunning = true
//设置触摸点 //设置触摸点
setTouchPoint(e2.x, e2.y) setTouchPoint(e2.x, e2.y)
@ -147,9 +147,29 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
return isMoved return isMoved
} }
override fun setDirection(direction: Direction) {
super.setDirection(direction)
when (direction) {
Direction.PREV -> {
//上一页滑动不出现对角
if (startX > viewWidth / 2.0) {
calcCornerXY(startX, viewHeight.toFloat())
} else {
calcCornerXY(viewWidth - startX, viewHeight.toFloat())
}
}
Direction.NEXT -> {
if (viewWidth / 2.0 > startX) {
calcCornerXY(viewWidth - startX, startY)
}
}
else -> Unit
}
}
override fun onScrollStart() { override fun onScrollStart() {
val distanceX: Float val distanceX: Float
when (direction) { when (mDirection) {
Direction.NEXT -> distanceX = Direction.NEXT -> distanceX =
if (isCancel) { if (isCancel) {
var dis = viewWidth - startX + touchX var dis = viewWidth - startX + touchX
@ -174,12 +194,12 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
override fun onScrollStop() { override fun onScrollStop() {
curPage.x = 0.toFloat() curPage.x = 0.toFloat()
if (!isCancel) { if (!isCancel) {
pageView.fillPage(direction) pageView.fillPage(mDirection)
} }
} }
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
if (direction === Direction.NEXT) { if (mDirection === Direction.NEXT) {
calcPoints() calcPoints()
drawCurrentPageArea(canvas, curBitmap, mPath0) drawCurrentPageArea(canvas, curBitmap, mPath0)
drawNextPageAreaAndShadow(canvas, nextBitmap) drawNextPageAreaAndShadow(canvas, nextBitmap)

@ -10,7 +10,7 @@ class SlidePageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
override fun onScrollStart() { override fun onScrollStart() {
val distanceX: Float val distanceX: Float
when (direction) { when (mDirection) {
Direction.NEXT -> distanceX = Direction.NEXT -> distanceX =
if (isCancel) { if (isCancel) {
var dis = viewWidth - startX + touchX var dis = viewWidth - startX + touchX
@ -35,8 +35,8 @@ class SlidePageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
val offsetX = touchX - startX val offsetX = touchX - startX
if ((direction == Direction.NEXT && offsetX > 0) if ((mDirection == Direction.NEXT && offsetX > 0)
|| (direction == Direction.PREV && offsetX < 0) || (mDirection == Direction.PREV && offsetX < 0)
) return ) return
val distanceX = if (offsetX > 0) offsetX - viewWidth else offsetX + viewWidth val distanceX = if (offsetX > 0) offsetX - viewWidth else offsetX + viewWidth
@ -49,8 +49,8 @@ class SlidePageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
override fun onScroll() { override fun onScroll() {
val offsetX = touchX - startX val offsetX = touchX - startX
if ((direction == Direction.NEXT && offsetX > 0) if ((mDirection == Direction.NEXT && offsetX > 0)
|| (direction == Direction.PREV && offsetX < 0) || (mDirection == Direction.PREV && offsetX < 0)
) return ) return
curPage.translationX = offsetX curPage.translationX = offsetX
@ -60,7 +60,7 @@ class SlidePageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
curPage.x = 0.toFloat() curPage.x = 0.toFloat()
if (!isCancel) { if (!isCancel) {
pageView.fillPage(direction) pageView.fillPage(mDirection)
} }
} }
} }
Loading…
Cancel
Save