pull/48/head
kunfei 5 years ago
parent 6edfd8f7a3
commit fbd962c07a
  1. 40
      app/src/main/java/io/legado/app/ui/widget/page/PageView.kt
  2. 6
      app/src/main/java/io/legado/app/ui/widget/page/curl/CurlRenderer.kt
  3. 13
      app/src/main/java/io/legado/app/ui/widget/page/curl/CurlView.kt
  4. 2
      app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt
  5. 85
      app/src/main/java/io/legado/app/ui/widget/page/delegate/SimulationPageDelegate.kt

@ -17,7 +17,7 @@ class PageView(context: Context, attrs: AttributeSet) :
ContentView.CallBack, ContentView.CallBack,
DataSource { DataSource {
var callback: CallBack? = null var callBack: CallBack? = null
var pageFactory: TextPageFactory? = null var pageFactory: TextPageFactory? = null
private var pageDelegate: PageDelegate? = null private var pageDelegate: PageDelegate? = null
@ -27,7 +27,7 @@ class PageView(context: Context, attrs: AttributeSet) :
var curlView: CurlView? = null var curlView: CurlView? = null
init { init {
callback = activity as? CallBack callBack = activity as? CallBack
prevPage = ContentView(context) prevPage = ContentView(context)
addView(prevPage) addView(prevPage)
nextPage = ContentView(context) nextPage = ContentView(context)
@ -111,7 +111,7 @@ class PageView(context: Context, attrs: AttributeSet) :
curPage?.setContent(it.currentPage()) curPage?.setContent(it.currentPage())
nextPage?.setContent(it.nextPage()) nextPage?.setContent(it.nextPage())
prevPage?.setContent(it.previousPage()) prevPage?.setContent(it.previousPage())
callback?.let { callback -> callBack?.let { callback ->
if (isScrollDelegate) { if (isScrollDelegate) {
curPage?.scrollTo(callback.textChapter()?.getStartLine(callback.durChapterPos())) curPage?.scrollTo(callback.textChapter()?.getStartLine(callback.durChapterPos()))
} }
@ -128,7 +128,7 @@ class PageView(context: Context, attrs: AttributeSet) :
fun moveToPrevPage(noAnim: Boolean = true) { fun moveToPrevPage(noAnim: Boolean = true) {
if (noAnim) { if (noAnim) {
if (isScrollDelegate) { if (isScrollDelegate) {
callback?.textChapter()?.let { callBack?.textChapter()?.let {
curPage?.scrollTo(it.getStartLine(pageIndex - 1)) curPage?.scrollTo(it.getStartLine(pageIndex - 1))
} }
} else { } else {
@ -140,7 +140,7 @@ class PageView(context: Context, attrs: AttributeSet) :
fun moveToNextPage(noAnim: Boolean = true) { fun moveToNextPage(noAnim: Boolean = true) {
if (noAnim) { if (noAnim) {
if (isScrollDelegate) { if (isScrollDelegate) {
callback?.textChapter()?.let { callBack?.textChapter()?.let {
curPage?.scrollTo(it.getStartLine(pageIndex + 1)) curPage?.scrollTo(it.getStartLine(pageIndex + 1))
} }
} else { } else {
@ -180,68 +180,68 @@ class PageView(context: Context, attrs: AttributeSet) :
get() = pageDelegate is ScrollPageDelegate get() = pageDelegate is ScrollPageDelegate
override val pageIndex: Int override val pageIndex: Int
get() = callback?.durChapterPos() ?: 0 get() = callBack?.durChapterPos() ?: 0
override fun setPageIndex(pageIndex: Int) { override fun setPageIndex(pageIndex: Int) {
callback?.setPageIndex(pageIndex) callBack?.setPageIndex(pageIndex)
} }
override fun getChapterPosition(): Int { override fun getChapterPosition(): Int {
return callback?.durChapterIndex() ?: 0 return callBack?.durChapterIndex() ?: 0
} }
override fun getChapter(position: Int): TextChapter? { override fun getChapter(position: Int): TextChapter? {
return callback?.textChapter(position) return callBack?.textChapter(position)
} }
override fun getCurrentChapter(): TextChapter? { override fun getCurrentChapter(): TextChapter? {
return callback?.textChapter(0) return callBack?.textChapter(0)
} }
override fun getNextChapter(): TextChapter? { override fun getNextChapter(): TextChapter? {
return callback?.textChapter(1) return callBack?.textChapter(1)
} }
override fun getPreviousChapter(): TextChapter? { override fun getPreviousChapter(): TextChapter? {
return callback?.textChapter(-1) return callBack?.textChapter(-1)
} }
override fun hasNextChapter(): Boolean { override fun hasNextChapter(): Boolean {
callback?.let { callBack?.let {
return it.durChapterIndex() < it.chapterSize() - 1 return it.durChapterIndex() < it.chapterSize() - 1
} }
return false return false
} }
override fun hasPrevChapter(): Boolean { override fun hasPrevChapter(): Boolean {
callback?.let { callBack?.let {
return it.durChapterIndex() > 0 return it.durChapterIndex() > 0
} }
return false return false
} }
override fun moveToNextChapter() { override fun moveToNextChapter() {
callback?.moveToNextChapter(false) callBack?.moveToNextChapter(false)
} }
override fun moveToPrevChapter() { override fun moveToPrevChapter() {
callback?.moveToPrevChapter(false) callBack?.moveToPrevChapter(false)
} }
override fun scrollToLine(line: Int) { override fun scrollToLine(line: Int) {
if (isScrollDelegate) { if (isScrollDelegate) {
callback?.textChapter()?.let { callBack?.textChapter()?.let {
val pageIndex = it.getPageIndex(line) val pageIndex = it.getPageIndex(line)
curPage?.setPageIndex(pageIndex) curPage?.setPageIndex(pageIndex)
callback?.setPageIndex(pageIndex) callBack?.setPageIndex(pageIndex)
} }
} }
} }
override fun scrollToLast() { override fun scrollToLast() {
if (isScrollDelegate) { if (isScrollDelegate) {
callback?.textChapter()?.let { callBack?.textChapter()?.let {
callback?.setPageIndex(it.lastIndex()) callBack?.setPageIndex(it.lastIndex())
curPage?.setPageIndex(it.lastIndex()) curPage?.setPageIndex(it.lastIndex())
} }
} }

@ -67,6 +67,10 @@ class CurlRenderer(private val mObserver: Observer) : GLSurfaceView.Renderer {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT) gl.glClear(GL10.GL_COLOR_BUFFER_BIT)
gl.glLoadIdentity() gl.glLoadIdentity()
if (!mObserver.canDraw) {
return
}
for (i in mCurlMeshes.indices) { for (i in mCurlMeshes.indices) {
mCurlMeshes[i].onDrawFrame(gl) mCurlMeshes[i].onDrawFrame(gl)
} }
@ -200,6 +204,8 @@ class CurlRenderer(private val mObserver: Observer) : GLSurfaceView.Renderer {
* what needs to be done when this happens. * what needs to be done when this happens.
*/ */
fun onSurfaceCreated() fun onSurfaceCreated()
var canDraw: Boolean
} }
companion object { companion object {

@ -18,7 +18,7 @@ import kotlin.math.sqrt
* @author harism * @author harism
*/ */
class CurlView : GLSurfaceView, View.OnTouchListener, CurlRenderer.Observer { class CurlView : GLSurfaceView, View.OnTouchListener, CurlRenderer.Observer {
var callBack: CallBack? = null
private var mAllowLastPageCurl = true private var mAllowLastPageCurl = true
private var mAnimate = false private var mAnimate = false
@ -133,6 +133,8 @@ class CurlView : GLSurfaceView, View.OnTouchListener, CurlRenderer.Observer {
mPageRight.setFlipTexture(false) mPageRight.setFlipTexture(false)
} }
override var canDraw: Boolean = false
override fun onDrawFrame() { override fun onDrawFrame() {
// We are not animating. // We are not animating.
if (!mAnimate) { if (!mAnimate) {
@ -155,7 +157,9 @@ class CurlView : GLSurfaceView, View.OnTouchListener, CurlRenderer.Observer {
// If we were curling left page update current index. // If we were curling left page update current index.
if (mCurlState == CURL_LEFT) { if (mCurlState == CURL_LEFT) {
--mCurrentIndex --mCurrentIndex
callBack?.pageChange(-1)
} }
canDraw = false
} else if (mAnimationTargetEvent == SET_CURL_TO_LEFT) { } else if (mAnimationTargetEvent == SET_CURL_TO_LEFT) {
// Switch curled page to left. // Switch curled page to left.
val left = mPageCurl val left = mPageCurl
@ -172,7 +176,9 @@ class CurlView : GLSurfaceView, View.OnTouchListener, CurlRenderer.Observer {
// If we were curling right page update current index. // If we were curling right page update current index.
if (mCurlState == CURL_RIGHT) { if (mCurlState == CURL_RIGHT) {
++mCurrentIndex ++mCurrentIndex
callBack?.pageChange(1)
} }
canDraw = false
} }
mCurlState = CURL_NONE mCurlState = CURL_NONE
mAnimate = false mAnimate = false
@ -187,6 +193,7 @@ class CurlView : GLSurfaceView, View.OnTouchListener, CurlRenderer.Observer {
} }
} }
override fun onPageSizeChanged(width: Int, height: Int) { override fun onPageSizeChanged(width: Int, height: Int) {
mPageBitmapWidth = width mPageBitmapWidth = width
mPageBitmapHeight = height mPageBitmapHeight = height
@ -738,6 +745,10 @@ class CurlView : GLSurfaceView, View.OnTouchListener, CurlRenderer.Observer {
fun onSizeChanged(width: Int, height: Int) fun onSizeChanged(width: Int, height: Int)
} }
interface CallBack {
fun pageChange(change: Int)
}
companion object { companion object {
// Curl state. We are flipping none, left or right page. // Curl state. We are flipping none, left or right page.

@ -253,7 +253,7 @@ 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.callback?.clickCenter() pageView.callBack?.clickCenter()
setTouchPoint(x, y) setTouchPoint(x, y)
} else { } else {
bitmap = if (x > viewWidth / 2) { bitmap = if (x > viewWidth / 2) {

@ -6,21 +6,31 @@ import io.legado.app.ui.widget.page.PageView
import io.legado.app.ui.widget.page.curl.CurlPage import io.legado.app.ui.widget.page.curl.CurlPage
import io.legado.app.ui.widget.page.curl.CurlView import io.legado.app.ui.widget.page.curl.CurlView
import io.legado.app.utils.screenshot import io.legado.app.utils.screenshot
import kotlin.math.abs
class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) { class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView),
CurlView.CallBack {
var curlView: CurlView? = null
init { init {
pageView.curlView ?: let { pageView.curlView ?: let {
pageView.curlView = CurlView(pageView.context) curlView = CurlView(pageView.context)
pageView.addView(pageView.curlView) pageView.curlView = curlView
pageView.curlView?.mPageProvider = PageProvider() pageView.addView(curlView)
pageView.curlView?.setSizeChangedObserver(SizeChangedObserver()) curlView?.mPageProvider = PageProvider()
pageView.curlView?.currentIndex = 1 curlView?.setSizeChangedObserver(SizeChangedObserver())
curlView?.callBack = this
} }
} }
override fun onTouch(event: MotionEvent): Boolean { override fun onTouch(event: MotionEvent): Boolean {
pageView.curlView?.dispatchTouchEvent(event) when (event.action) {
MotionEvent.ACTION_DOWN -> {
curlView?.currentIndex = 1
}
}
curlView?.dispatchTouchEvent(event)
return super.onTouch(event) return super.onTouch(event)
} }
@ -33,9 +43,64 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
override fun onScrollStop() { override fun onScrollStop() {
} }
override fun onScroll(
e1: MotionEvent,
e2: MotionEvent,
distanceX: Float,
distanceY: Float
): Boolean {
if (!isMoved) {
val event = e1.toAction(MotionEvent.ACTION_UP)
curPage?.dispatchTouchEvent(event)
event.recycle()
if (abs(distanceX) > abs(distanceY)) {
if (distanceX < 0) {
//如果上一页不存在
if (!hasPrev()) {
noNext = true
return true
}
//上一页截图
bitmap = prevPage?.screenshot()
} else {
//如果不存在表示没有下一页了
if (!hasNext()) {
noNext = true
return true
}
//下一页截图
bitmap = nextPage?.screenshot()
}
isMoved = true
}
}
if (isMoved) {
curlView?.canDraw = true
isCancel = if (pageView.isScrollDelegate) {
if (direction == Direction.NEXT) distanceY < 0 else distanceY > 0
} else {
if (direction == Direction.NEXT) distanceX < 0 else distanceX > 0
}
isRunning = true
//设置触摸点
setTouchPoint(e2.x, e2.y)
}
return isMoved
}
override fun onPageUp() { override fun onPageUp() {
pageView.curlView?.updatePages() curlView?.updatePages()
pageView.curlView?.requestRender() curlView?.requestRender()
}
override fun pageChange(change: Int) {
pageView.post {
if (change > 0) {
pageView.moveToNextPage()
} else {
pageView.moveToPrevPage()
}
}
} }
private inner class PageProvider : CurlView.PageProvider { private inner class PageProvider : CurlView.PageProvider {
@ -55,7 +120,7 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
// 定义书籍尺寸的变化监听器 // 定义书籍尺寸的变化监听器
private inner class SizeChangedObserver : CurlView.SizeChangedObserver { private inner class SizeChangedObserver : CurlView.SizeChangedObserver {
override fun onSizeChanged(width: Int, height: Int) { override fun onSizeChanged(width: Int, height: Int) {
pageView.curlView?.setViewMode(CurlView.SHOW_ONE_PAGE) curlView?.setViewMode(CurlView.SHOW_ONE_PAGE)
} }
} }
} }
Loading…
Cancel
Save