From fbd962c07a905b1476804abc0167a23316612bf1 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 14 Nov 2019 16:34:30 +0800 Subject: [PATCH] up --- .../io/legado/app/ui/widget/page/PageView.kt | 40 ++++----- .../app/ui/widget/page/curl/CurlRenderer.kt | 6 ++ .../app/ui/widget/page/curl/CurlView.kt | 13 ++- .../ui/widget/page/delegate/PageDelegate.kt | 2 +- .../page/delegate/SimulationPageDelegate.kt | 85 ++++++++++++++++--- 5 files changed, 114 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt b/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt index e1e865830..3a8d914ce 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt @@ -17,7 +17,7 @@ class PageView(context: Context, attrs: AttributeSet) : ContentView.CallBack, DataSource { - var callback: CallBack? = null + var callBack: CallBack? = null var pageFactory: TextPageFactory? = null private var pageDelegate: PageDelegate? = null @@ -27,7 +27,7 @@ class PageView(context: Context, attrs: AttributeSet) : var curlView: CurlView? = null init { - callback = activity as? CallBack + callBack = activity as? CallBack prevPage = ContentView(context) addView(prevPage) nextPage = ContentView(context) @@ -111,7 +111,7 @@ class PageView(context: Context, attrs: AttributeSet) : curPage?.setContent(it.currentPage()) nextPage?.setContent(it.nextPage()) prevPage?.setContent(it.previousPage()) - callback?.let { callback -> + callBack?.let { callback -> if (isScrollDelegate) { curPage?.scrollTo(callback.textChapter()?.getStartLine(callback.durChapterPos())) } @@ -128,7 +128,7 @@ class PageView(context: Context, attrs: AttributeSet) : fun moveToPrevPage(noAnim: Boolean = true) { if (noAnim) { if (isScrollDelegate) { - callback?.textChapter()?.let { + callBack?.textChapter()?.let { curPage?.scrollTo(it.getStartLine(pageIndex - 1)) } } else { @@ -140,7 +140,7 @@ class PageView(context: Context, attrs: AttributeSet) : fun moveToNextPage(noAnim: Boolean = true) { if (noAnim) { if (isScrollDelegate) { - callback?.textChapter()?.let { + callBack?.textChapter()?.let { curPage?.scrollTo(it.getStartLine(pageIndex + 1)) } } else { @@ -180,68 +180,68 @@ class PageView(context: Context, attrs: AttributeSet) : get() = pageDelegate is ScrollPageDelegate override val pageIndex: Int - get() = callback?.durChapterPos() ?: 0 + get() = callBack?.durChapterPos() ?: 0 override fun setPageIndex(pageIndex: Int) { - callback?.setPageIndex(pageIndex) + callBack?.setPageIndex(pageIndex) } override fun getChapterPosition(): Int { - return callback?.durChapterIndex() ?: 0 + return callBack?.durChapterIndex() ?: 0 } override fun getChapter(position: Int): TextChapter? { - return callback?.textChapter(position) + return callBack?.textChapter(position) } override fun getCurrentChapter(): TextChapter? { - return callback?.textChapter(0) + return callBack?.textChapter(0) } override fun getNextChapter(): TextChapter? { - return callback?.textChapter(1) + return callBack?.textChapter(1) } override fun getPreviousChapter(): TextChapter? { - return callback?.textChapter(-1) + return callBack?.textChapter(-1) } override fun hasNextChapter(): Boolean { - callback?.let { + callBack?.let { return it.durChapterIndex() < it.chapterSize() - 1 } return false } override fun hasPrevChapter(): Boolean { - callback?.let { + callBack?.let { return it.durChapterIndex() > 0 } return false } override fun moveToNextChapter() { - callback?.moveToNextChapter(false) + callBack?.moveToNextChapter(false) } override fun moveToPrevChapter() { - callback?.moveToPrevChapter(false) + callBack?.moveToPrevChapter(false) } override fun scrollToLine(line: Int) { if (isScrollDelegate) { - callback?.textChapter()?.let { + callBack?.textChapter()?.let { val pageIndex = it.getPageIndex(line) curPage?.setPageIndex(pageIndex) - callback?.setPageIndex(pageIndex) + callBack?.setPageIndex(pageIndex) } } } override fun scrollToLast() { if (isScrollDelegate) { - callback?.textChapter()?.let { - callback?.setPageIndex(it.lastIndex()) + callBack?.textChapter()?.let { + callBack?.setPageIndex(it.lastIndex()) curPage?.setPageIndex(it.lastIndex()) } } diff --git a/app/src/main/java/io/legado/app/ui/widget/page/curl/CurlRenderer.kt b/app/src/main/java/io/legado/app/ui/widget/page/curl/CurlRenderer.kt index 041b2795b..aeec2472b 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/curl/CurlRenderer.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/curl/CurlRenderer.kt @@ -67,6 +67,10 @@ class CurlRenderer(private val mObserver: Observer) : GLSurfaceView.Renderer { gl.glClear(GL10.GL_COLOR_BUFFER_BIT) gl.glLoadIdentity() + if (!mObserver.canDraw) { + return + } + for (i in mCurlMeshes.indices) { mCurlMeshes[i].onDrawFrame(gl) } @@ -200,6 +204,8 @@ class CurlRenderer(private val mObserver: Observer) : GLSurfaceView.Renderer { * what needs to be done when this happens. */ fun onSurfaceCreated() + + var canDraw: Boolean } companion object { diff --git a/app/src/main/java/io/legado/app/ui/widget/page/curl/CurlView.kt b/app/src/main/java/io/legado/app/ui/widget/page/curl/CurlView.kt index 2b62974ef..1124d71d0 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/curl/CurlView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/curl/CurlView.kt @@ -18,7 +18,7 @@ import kotlin.math.sqrt * @author harism */ class CurlView : GLSurfaceView, View.OnTouchListener, CurlRenderer.Observer { - + var callBack: CallBack? = null private var mAllowLastPageCurl = true private var mAnimate = false @@ -133,6 +133,8 @@ class CurlView : GLSurfaceView, View.OnTouchListener, CurlRenderer.Observer { mPageRight.setFlipTexture(false) } + override var canDraw: Boolean = false + override fun onDrawFrame() { // We are not animating. if (!mAnimate) { @@ -155,7 +157,9 @@ class CurlView : GLSurfaceView, View.OnTouchListener, CurlRenderer.Observer { // If we were curling left page update current index. if (mCurlState == CURL_LEFT) { --mCurrentIndex + callBack?.pageChange(-1) } + canDraw = false } else if (mAnimationTargetEvent == SET_CURL_TO_LEFT) { // Switch curled page to left. val left = mPageCurl @@ -172,7 +176,9 @@ class CurlView : GLSurfaceView, View.OnTouchListener, CurlRenderer.Observer { // If we were curling right page update current index. if (mCurlState == CURL_RIGHT) { ++mCurrentIndex + callBack?.pageChange(1) } + canDraw = false } mCurlState = CURL_NONE mAnimate = false @@ -187,6 +193,7 @@ class CurlView : GLSurfaceView, View.OnTouchListener, CurlRenderer.Observer { } } + override fun onPageSizeChanged(width: Int, height: Int) { mPageBitmapWidth = width mPageBitmapHeight = height @@ -738,6 +745,10 @@ class CurlView : GLSurfaceView, View.OnTouchListener, CurlRenderer.Observer { fun onSizeChanged(width: Int, height: Int) } + interface CallBack { + fun pageChange(change: Int) + } + companion object { // Curl state. We are flipping none, left or right page. diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt index d094e4a82..0c6585df8 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt @@ -253,7 +253,7 @@ abstract class PageDelegate(protected val pageView: PageView) { val x = e.x val y = e.y if (centerRectF.contains(x, y)) { - pageView.callback?.clickCenter() + pageView.callBack?.clickCenter() setTouchPoint(x, y) } else { bitmap = if (x > viewWidth / 2) { diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/SimulationPageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/SimulationPageDelegate.kt index e28b63509..a83d548a2 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/delegate/SimulationPageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/SimulationPageDelegate.kt @@ -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.CurlView 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 { pageView.curlView ?: let { - pageView.curlView = CurlView(pageView.context) - pageView.addView(pageView.curlView) - pageView.curlView?.mPageProvider = PageProvider() - pageView.curlView?.setSizeChangedObserver(SizeChangedObserver()) - pageView.curlView?.currentIndex = 1 + curlView = CurlView(pageView.context) + pageView.curlView = curlView + pageView.addView(curlView) + curlView?.mPageProvider = PageProvider() + curlView?.setSizeChangedObserver(SizeChangedObserver()) + curlView?.callBack = this } } 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) } @@ -33,9 +43,64 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi 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() { - pageView.curlView?.updatePages() - pageView.curlView?.requestRender() + curlView?.updatePages() + curlView?.requestRender() + } + + override fun pageChange(change: Int) { + pageView.post { + if (change > 0) { + pageView.moveToNextPage() + } else { + pageView.moveToPrevPage() + } + } } private inner class PageProvider : CurlView.PageProvider { @@ -55,7 +120,7 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi // 定义书籍尺寸的变化监听器 private inner class SizeChangedObserver : CurlView.SizeChangedObserver { override fun onSizeChanged(width: Int, height: Int) { - pageView.curlView?.setViewMode(CurlView.SHOW_ONE_PAGE) + curlView?.setViewMode(CurlView.SHOW_ONE_PAGE) } } } \ No newline at end of file