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

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

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

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

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

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

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