pull/34/head
kunfei 5 years ago
parent e846858488
commit 438be8a1c1
  1. 17
      app/src/main/java/io/legado/app/ui/widget/page/PageView.kt
  2. 63
      app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt

@ -13,13 +13,12 @@ import io.legado.app.utils.getPrefInt
class PageView(context: Context, attrs: AttributeSet) : class PageView(context: Context, attrs: AttributeSet) :
FrameLayout(context, attrs), FrameLayout(context, attrs),
PageDelegate.PageInterface,
ContentView.CallBack, ContentView.CallBack,
DataSource { DataSource {
private var callback: CallBack? = null var callback: CallBack? = null
var pageFactory: TextPageFactory? = null
private var pageDelegate: PageDelegate? = null private var pageDelegate: PageDelegate? = null
private var pageFactory: TextPageFactory? = null
var prevPage: ContentView? = null var prevPage: ContentView? = null
var curPage: ContentView? = null var curPage: ContentView? = null
@ -169,18 +168,6 @@ class PageView(context: Context, attrs: AttributeSet) :
nextPage?.upBattery(battery) nextPage?.upBattery(battery)
} }
override fun hasNext(): Boolean {
return pageFactory?.hasNext() == true
}
override fun hasPrev(): Boolean {
return pageFactory?.hasPrev() == true
}
override fun clickCenter() {
callback?.clickCenter()
}
override val isScrollDelegate: Boolean override val isScrollDelegate: Boolean
get() = pageDelegate is ScrollPageDelegate get() = pageDelegate is ScrollPageDelegate

@ -104,6 +104,7 @@ abstract class PageDelegate(protected val pageView: PageView) {
protected fun stopScroll() { protected fun stopScroll() {
isRunning = false isRunning = false
isStarted = false isStarted = false
invalidate()
if (pageView.isScrollDelegate) { if (pageView.isScrollDelegate) {
pageView.postDelayed({ pageView.postDelayed({
bitmap?.recycle() bitmap?.recycle()
@ -113,7 +114,6 @@ abstract class PageDelegate(protected val pageView: PageView) {
bitmap?.recycle() bitmap?.recycle()
bitmap = null bitmap = null
} }
invalidate()
} }
fun setViewSize(width: Int, height: Int) { fun setViewSize(width: Int, height: Int) {
@ -144,7 +144,6 @@ abstract class PageDelegate(protected val pageView: PageView) {
fun start(direction: Direction) { fun start(direction: Direction) {
if (isStarted) return if (isStarted) return
this.direction = direction
if (direction === Direction.NEXT) { if (direction === Direction.NEXT) {
val x = viewWidth.toFloat() val x = viewWidth.toFloat()
val y = viewHeight.toFloat() val y = viewHeight.toFloat()
@ -153,8 +152,7 @@ abstract class PageDelegate(protected val pageView: PageView) {
//设置点击点 //设置点击点
setTouchPoint(x, y, false) setTouchPoint(x, y, false)
//设置方向 //设置方向
val hasNext = pageView.hasNext() if (!hasNext()) {
if (!hasNext) {
return return
} }
} else { } else {
@ -165,8 +163,7 @@ abstract class PageDelegate(protected val pageView: PageView) {
//设置点击点 //设置点击点
setTouchPoint(x, y, false) setTouchPoint(x, y, false)
//设置方向方向 //设置方向方向
val hashPrev = pageView.hasPrev() if (!hasPrev()) {
if (!hashPrev) {
return return
} }
} }
@ -240,26 +237,22 @@ abstract class PageDelegate(protected val pageView: PageView) {
val x = e.x val x = e.x
val y = e.y val y = e.y
if (centerRectF.contains(x, y)) { if (centerRectF.contains(x, y)) {
pageView.clickCenter() pageView.callback?.clickCenter()
setTouchPoint(x, y) setTouchPoint(x, y)
} else { } else {
direction = if (x > viewWidth / 2) Direction.NEXT else Direction.PREV bitmap = if (x > viewWidth / 2) {
if (direction == Direction.NEXT) {
//判断是否下一页存在
val hasNext = pageView.hasNext()
//设置动画方向 //设置动画方向
if (!hasNext) { if (!hasNext()) {
return true return true
} }
//下一页截图 //下一页截图
bitmap = nextPage?.screenshot() nextPage?.screenshot()
} else { } else {
val hasPrev = pageView.hasPrev() if (!hasPrev()) {
if (!hasPrev) {
return true return true
} }
//上一页截图 //上一页截图
bitmap = prevPage?.screenshot() prevPage?.screenshot()
} }
setTouchPoint(x, y) setTouchPoint(x, y)
onScrollStart() onScrollStart()
@ -280,12 +273,8 @@ abstract class PageDelegate(protected val pageView: PageView) {
val event = e1.toAction(MotionEvent.ACTION_UP) val event = e1.toAction(MotionEvent.ACTION_UP)
curPage?.dispatchTouchEvent(event) curPage?.dispatchTouchEvent(event)
event.recycle() event.recycle()
//上一页的参数配置
direction = Direction.PREV
//判断是否上一页存在
val hasPrev = pageView.hasPrev()
//如果上一页不存在 //如果上一页不存在
if (!hasPrev) { if (!hasPrev()) {
noNext = true noNext = true
return true return true
} }
@ -297,12 +286,8 @@ abstract class PageDelegate(protected val pageView: PageView) {
val event = e1.toAction(MotionEvent.ACTION_UP) val event = e1.toAction(MotionEvent.ACTION_UP)
curPage?.dispatchTouchEvent(event) curPage?.dispatchTouchEvent(event)
event.recycle() event.recycle()
//进行下一页的配置
direction = Direction.NEXT
//判断是否下一页存在
val hasNext = pageView.hasNext()
//如果不存在表示没有下一页了 //如果不存在表示没有下一页了
if (!hasNext) { if (!hasNext()) {
noNext = true noNext = true
return true return true
} }
@ -322,24 +307,16 @@ abstract class PageDelegate(protected val pageView: PageView) {
event.recycle() event.recycle()
if (abs(distanceX) > abs(distanceY)) { if (abs(distanceX) > abs(distanceY)) {
if (distanceX < 0) { if (distanceX < 0) {
//上一页的参数配置
direction = Direction.PREV
//判断是否上一页存在
val hasPrev = pageView.hasPrev()
//如果上一页不存在 //如果上一页不存在
if (!hasPrev) { if (!hasPrev()) {
noNext = true noNext = true
return true return true
} }
//上一页截图 //上一页截图
bitmap = prevPage?.screenshot() bitmap = prevPage?.screenshot()
} else { } else {
//进行下一页的配置
direction = Direction.NEXT
//判断是否下一页存在
val hasNext = pageView.hasNext()
//如果不存在表示没有下一页了 //如果不存在表示没有下一页了
if (!hasNext) { if (!hasNext()) {
noNext = true noNext = true
return true return true
} }
@ -363,9 +340,15 @@ abstract class PageDelegate(protected val pageView: PageView) {
} }
} }
interface PageInterface { private fun hasPrev(): Boolean {
fun hasNext(): Boolean //上一页的参数配置
fun hasPrev(): Boolean direction = Direction.PREV
fun clickCenter() return pageView.pageFactory?.hasPrev() == true
}
private fun hasNext(): Boolean {
//进行下一页的配置
direction = Direction.NEXT
return pageView.pageFactory?.hasNext() == true
} }
} }
Loading…
Cancel
Save