pull/497/head
gedoor 4 years ago
parent 9d4e31a488
commit 55f0b77e87
  1. 18
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 7
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt
  3. 11
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/CoverPageDelegate.kt
  4. 17
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt
  5. 17
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt
  6. 21
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/SimulationPageDelegate.kt
  7. 11
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/SlidePageDelegate.kt
  8. 5
      app/src/main/java/io/legado/app/ui/book/read/page/entities/PageDirection.kt

@ -37,7 +37,7 @@ import io.legado.app.ui.book.read.config.BgTextConfigDialog.Companion.TEXT_COLOR
import io.legado.app.ui.book.read.page.ContentTextView import io.legado.app.ui.book.read.page.ContentTextView
import io.legado.app.ui.book.read.page.PageView import io.legado.app.ui.book.read.page.PageView
import io.legado.app.ui.book.read.page.TextPageFactory import io.legado.app.ui.book.read.page.TextPageFactory
import io.legado.app.ui.book.read.page.delegate.PageDelegate import io.legado.app.ui.book.read.page.entities.PageDirection
import io.legado.app.ui.book.searchContent.SearchContentActivity import io.legado.app.ui.book.searchContent.SearchContentActivity
import io.legado.app.ui.book.toc.ChapterListActivity import io.legado.app.ui.book.toc.ChapterListActivity
import io.legado.app.ui.login.SourceLogin import io.legado.app.ui.login.SourceLogin
@ -260,28 +260,28 @@ class ReadBookActivity : ReadBookBaseActivity(),
when { when {
isPrevKey(keyCode) -> { isPrevKey(keyCode) -> {
if (keyCode != KeyEvent.KEYCODE_UNKNOWN) { if (keyCode != KeyEvent.KEYCODE_UNKNOWN) {
page_view.pageDelegate?.keyTurnPage(PageDelegate.Direction.PREV) page_view.pageDelegate?.keyTurnPage(PageDirection.PREV)
return true return true
} }
} }
isNextKey(keyCode) -> { isNextKey(keyCode) -> {
if (keyCode != KeyEvent.KEYCODE_UNKNOWN) { if (keyCode != KeyEvent.KEYCODE_UNKNOWN) {
page_view.pageDelegate?.keyTurnPage(PageDelegate.Direction.NEXT) page_view.pageDelegate?.keyTurnPage(PageDirection.NEXT)
return true return true
} }
} }
keyCode == KeyEvent.KEYCODE_VOLUME_UP -> { keyCode == KeyEvent.KEYCODE_VOLUME_UP -> {
if (volumeKeyPage(PageDelegate.Direction.PREV)) { if (volumeKeyPage(PageDirection.PREV)) {
return true return true
} }
} }
keyCode == KeyEvent.KEYCODE_VOLUME_DOWN -> { keyCode == KeyEvent.KEYCODE_VOLUME_DOWN -> {
if (volumeKeyPage(PageDelegate.Direction.NEXT)) { if (volumeKeyPage(PageDirection.NEXT)) {
return true return true
} }
} }
keyCode == KeyEvent.KEYCODE_SPACE -> { keyCode == KeyEvent.KEYCODE_SPACE -> {
page_view.pageDelegate?.keyTurnPage(PageDelegate.Direction.NEXT) page_view.pageDelegate?.keyTurnPage(PageDirection.NEXT)
return true return true
} }
} }
@ -307,7 +307,7 @@ class ReadBookActivity : ReadBookBaseActivity(),
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean { override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
when (keyCode) { when (keyCode) {
KeyEvent.KEYCODE_VOLUME_UP, KeyEvent.KEYCODE_VOLUME_DOWN -> { KeyEvent.KEYCODE_VOLUME_UP, KeyEvent.KEYCODE_VOLUME_DOWN -> {
if (volumeKeyPage(PageDelegate.Direction.NONE)) { if (volumeKeyPage(PageDirection.NONE)) {
return true return true
} }
} }
@ -458,7 +458,7 @@ class ReadBookActivity : ReadBookBaseActivity(),
/** /**
* 音量键翻页 * 音量键翻页
*/ */
private fun volumeKeyPage(direction: PageDelegate.Direction): Boolean { private fun volumeKeyPage(direction: PageDirection): Boolean {
if (!read_menu.isVisible) { if (!read_menu.isVisible) {
if (getPrefBoolean("volumeKeyPage", true)) { if (getPrefBoolean("volumeKeyPage", true)) {
if (getPrefBoolean("volumeKeyPageOnPlay") if (getPrefBoolean("volumeKeyPageOnPlay")
@ -612,7 +612,7 @@ class ReadBookActivity : ReadBookBaseActivity(),
autoPageProgress++ autoPageProgress++
if (autoPageProgress >= ReadBookConfig.autoReadSpeed * 50) { if (autoPageProgress >= ReadBookConfig.autoReadSpeed * 50) {
autoPageProgress = 0 autoPageProgress = 0
page_view.fillPage(PageDelegate.Direction.NEXT) page_view.fillPage(PageDirection.NEXT)
} else { } else {
page_view.invalidate() page_view.invalidate()
} }

@ -15,6 +15,7 @@ import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.accentColor
import io.legado.app.service.help.ReadBook import io.legado.app.service.help.ReadBook
import io.legado.app.ui.book.read.page.delegate.* import io.legado.app.ui.book.read.page.delegate.*
import io.legado.app.ui.book.read.page.entities.PageDirection
import io.legado.app.ui.book.read.page.entities.TextChapter import io.legado.app.ui.book.read.page.entities.TextChapter
import io.legado.app.ui.book.read.page.provider.ChapterProvider import io.legado.app.ui.book.read.page.provider.ChapterProvider
import io.legado.app.utils.activity import io.legado.app.utils.activity
@ -339,12 +340,12 @@ class PageView(context: Context, attrs: AttributeSet) :
curPage.cancelSelect() curPage.cancelSelect()
} }
fun fillPage(direction: PageDelegate.Direction) { fun fillPage(direction: PageDirection) {
when (direction) { when (direction) {
PageDelegate.Direction.PREV -> { PageDirection.PREV -> {
pageFactory.moveToPrev(true) pageFactory.moveToPrev(true)
} }
PageDelegate.Direction.NEXT -> { PageDirection.NEXT -> {
pageFactory.moveToNext(true) pageFactory.moveToNext(true)
} }
else -> Unit else -> Unit

@ -4,6 +4,7 @@ import android.graphics.Canvas
import android.graphics.Matrix import android.graphics.Matrix
import android.graphics.drawable.GradientDrawable import android.graphics.drawable.GradientDrawable
import io.legado.app.ui.book.read.page.PageView import io.legado.app.ui.book.read.page.PageView
import io.legado.app.ui.book.read.page.entities.PageDirection
class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) { class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
private val bitmapMatrix = Matrix() private val bitmapMatrix = Matrix()
@ -21,19 +22,19 @@ class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
if (!isRunning) return if (!isRunning) return
val offsetX = touchX - startX val offsetX = touchX - startX
if ((mDirection == Direction.NEXT && offsetX > 0) if ((mDirection == PageDirection.NEXT && offsetX > 0)
|| (mDirection == Direction.PREV && offsetX < 0) || (mDirection == PageDirection.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
if (mDirection == Direction.PREV) { if (mDirection == PageDirection.PREV) {
bitmapMatrix.setTranslate(distanceX, 0.toFloat()) bitmapMatrix.setTranslate(distanceX, 0.toFloat())
curBitmap?.let { canvas.drawBitmap(it, 0f, 0f, null) } curBitmap?.let { canvas.drawBitmap(it, 0f, 0f, null) }
prevBitmap?.let { canvas.drawBitmap(it, bitmapMatrix, null) } prevBitmap?.let { canvas.drawBitmap(it, bitmapMatrix, null) }
addShadow(distanceX.toInt(), canvas) addShadow(distanceX.toInt(), canvas)
} else if (mDirection == Direction.NEXT) { } else if (mDirection == PageDirection.NEXT) {
bitmapMatrix.setTranslate(distanceX - viewWidth, 0.toFloat()) bitmapMatrix.setTranslate(distanceX - viewWidth, 0.toFloat())
nextBitmap?.let { canvas.drawBitmap(it, 0f, 0f, null) } nextBitmap?.let { canvas.drawBitmap(it, 0f, 0f, null) }
curBitmap?.let { canvas.drawBitmap(it, bitmapMatrix, null) } curBitmap?.let { canvas.drawBitmap(it, bitmapMatrix, null) }
@ -60,7 +61,7 @@ class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
override fun onAnimStart(animationSpeed: Int) { override fun onAnimStart(animationSpeed: Int) {
val distanceX: Float val distanceX: Float
when (mDirection) { when (mDirection) {
Direction.NEXT -> distanceX = PageDirection.NEXT -> distanceX =
if (isCancel) { if (isCancel) {
var dis = viewWidth - startX + touchX var dis = viewWidth - startX + touchX
if (dis > viewWidth) { if (dis > viewWidth) {

@ -3,6 +3,7 @@ package io.legado.app.ui.book.read.page.delegate
import android.graphics.Bitmap import android.graphics.Bitmap
import android.view.MotionEvent import android.view.MotionEvent
import io.legado.app.ui.book.read.page.PageView import io.legado.app.ui.book.read.page.PageView
import io.legado.app.ui.book.read.page.entities.PageDirection
import io.legado.app.utils.screenshot import io.legado.app.utils.screenshot
abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageView) { abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageView) {
@ -11,20 +12,20 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
protected var prevBitmap: Bitmap? = null protected var prevBitmap: Bitmap? = null
protected var nextBitmap: Bitmap? = null protected var nextBitmap: Bitmap? = null
override fun setDirection(direction: Direction) { override fun setDirection(direction: PageDirection) {
super.setDirection(direction) super.setDirection(direction)
setBitmap() setBitmap()
} }
private fun setBitmap() { private fun setBitmap() {
when (mDirection) { when (mDirection) {
Direction.PREV -> { PageDirection.PREV -> {
prevBitmap?.recycle() prevBitmap?.recycle()
prevBitmap = prevPage.screenshot() prevBitmap = prevPage.screenshot()
curBitmap?.recycle() curBitmap?.recycle()
curBitmap = curPage.screenshot() curBitmap = curPage.screenshot()
} }
Direction.NEXT -> { PageDirection.NEXT -> {
nextBitmap?.recycle() nextBitmap?.recycle()
nextBitmap = nextPage.screenshot() nextBitmap = nextPage.screenshot()
curBitmap?.recycle() curBitmap?.recycle()
@ -79,19 +80,19 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
noNext = true noNext = true
return return
} }
setDirection(Direction.PREV) setDirection(PageDirection.PREV)
} else { } else {
//如果不存在表示没有下一页了 //如果不存在表示没有下一页了
if (!hasNext()) { if (!hasNext()) {
noNext = true noNext = true
return return
} }
setDirection(Direction.NEXT) setDirection(PageDirection.NEXT)
} }
} }
} }
if (isMoved) { if (isMoved) {
isCancel = if (mDirection == Direction.NEXT) sumX > lastX else sumX < lastX isCancel = if (mDirection == PageDirection.NEXT) sumX > lastX else sumX < lastX
isRunning = true isRunning = true
//设置触摸点 //设置触摸点
pageView.setTouchPoint(sumX, sumY) pageView.setTouchPoint(sumX, sumY)
@ -117,7 +118,7 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
override fun nextPageByAnim(animationSpeed: Int) { override fun nextPageByAnim(animationSpeed: Int) {
abortAnim() abortAnim()
if (!hasNext()) return if (!hasNext()) return
setDirection(Direction.NEXT) setDirection(PageDirection.NEXT)
pageView.setTouchPoint(viewWidth.toFloat(), 0f, false) pageView.setTouchPoint(viewWidth.toFloat(), 0f, false)
onAnimStart(animationSpeed) onAnimStart(animationSpeed)
} }
@ -125,7 +126,7 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
override fun prevPageByAnim(animationSpeed: Int) { override fun prevPageByAnim(animationSpeed: Int) {
abortAnim() abortAnim()
if (!hasPrev()) return if (!hasPrev()) return
setDirection(Direction.PREV) setDirection(PageDirection.PREV)
pageView.setTouchPoint(0f, 0f) pageView.setTouchPoint(0f, 0f)
onAnimStart(animationSpeed) onAnimStart(animationSpeed)
} }

@ -10,6 +10,7 @@ import com.google.android.material.snackbar.Snackbar
import io.legado.app.R import io.legado.app.R
import io.legado.app.ui.book.read.page.ContentView import io.legado.app.ui.book.read.page.ContentView
import io.legado.app.ui.book.read.page.PageView import io.legado.app.ui.book.read.page.PageView
import io.legado.app.ui.book.read.page.entities.PageDirection
import kotlin.math.abs import kotlin.math.abs
abstract class PageDelegate(protected val pageView: PageView) { abstract class PageDelegate(protected val pageView: PageView) {
@ -47,7 +48,7 @@ abstract class PageDelegate(protected val pageView: PageView) {
var noNext = true var noNext = true
//移动方向 //移动方向
var mDirection = Direction.NONE var mDirection = PageDirection.NONE
var isCancel = false var isCancel = false
var isRunning = false var isRunning = false
var isStarted = false var isStarted = false
@ -117,17 +118,17 @@ abstract class PageDelegate(protected val pageView: PageView) {
abstract fun prevPageByAnim(animationSpeed: Int) abstract fun prevPageByAnim(animationSpeed: Int)
open fun keyTurnPage(direction: Direction) { open fun keyTurnPage(direction: PageDirection) {
if (isRunning) return if (isRunning) return
when (direction) { when (direction) {
Direction.NEXT -> nextPageByAnim(100) PageDirection.NEXT -> nextPageByAnim(100)
Direction.PREV -> prevPageByAnim(100) PageDirection.PREV -> prevPageByAnim(100)
else -> return else -> return
} }
} }
@CallSuper @CallSuper
open fun setDirection(direction: Direction) { open fun setDirection(direction: PageDirection) {
mDirection = direction mDirection = direction
} }
@ -149,7 +150,7 @@ abstract class PageDelegate(protected val pageView: PageView) {
//取消 //取消
isCancel = false isCancel = false
//是下一章还是前一章 //是下一章还是前一章
setDirection(Direction.NONE) setDirection(PageDirection.NONE)
} }
/** /**
@ -184,8 +185,4 @@ abstract class PageDelegate(protected val pageView: PageView) {
} }
enum class Direction {
NONE, PREV, NEXT
}
} }

@ -6,6 +6,7 @@ import android.os.Build
import android.view.MotionEvent import android.view.MotionEvent
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.ui.book.read.page.PageView import io.legado.app.ui.book.read.page.PageView
import io.legado.app.ui.book.read.page.entities.PageDirection
import kotlin.math.* import kotlin.math.*
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
@ -123,13 +124,13 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
} }
MotionEvent.ACTION_MOVE -> { MotionEvent.ACTION_MOVE -> {
if ((startY > viewHeight / 3 && startY < viewHeight * 2 / 3) if ((startY > viewHeight / 3 && startY < viewHeight * 2 / 3)
|| mDirection == Direction.PREV || mDirection == PageDirection.PREV
) { ) {
pageView.touchY = viewHeight.toFloat() pageView.touchY = viewHeight.toFloat()
} }
if (startY > viewHeight / 3 && startY < viewHeight / 2 if (startY > viewHeight / 3 && startY < viewHeight / 2
&& mDirection == Direction.NEXT && mDirection == PageDirection.NEXT
) { ) {
pageView.touchY = 1f pageView.touchY = 1f
} }
@ -137,17 +138,17 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
} }
} }
override fun setDirection(direction: Direction) { override fun setDirection(direction: PageDirection) {
super.setDirection(direction) super.setDirection(direction)
when (direction) { when (direction) {
Direction.PREV -> PageDirection.PREV ->
//上一页滑动不出现对角 //上一页滑动不出现对角
if (startX > viewWidth / 2) { if (startX > viewWidth / 2) {
calcCornerXY(startX, viewHeight.toFloat()) calcCornerXY(startX, viewHeight.toFloat())
} else { } else {
calcCornerXY(viewWidth - startX, viewHeight.toFloat()) calcCornerXY(viewWidth - startX, viewHeight.toFloat())
} }
Direction.NEXT -> PageDirection.NEXT ->
if (viewWidth / 2 > startX) { if (viewWidth / 2 > startX) {
calcCornerXY(viewWidth - startX, startY) calcCornerXY(viewWidth - startX, startY)
} }
@ -160,12 +161,12 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
val dy: Float val dy: Float
// dy 垂直方向滑动的距离,负值会使滚动向上滚动 // dy 垂直方向滑动的距离,负值会使滚动向上滚动
if (isCancel) { if (isCancel) {
dx = if (mCornerX > 0 && mDirection == Direction.NEXT) { dx = if (mCornerX > 0 && mDirection == PageDirection.NEXT) {
(viewWidth - touchX) (viewWidth - touchX)
} else { } else {
-touchX -touchX
} }
if (mDirection != Direction.NEXT) { if (mDirection != PageDirection.NEXT) {
dx = -(viewWidth + touchX) dx = -(viewWidth + touchX)
} }
dy = if (mCornerY > 0) { dy = if (mCornerY > 0) {
@ -174,7 +175,7 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
-touchY // 防止mTouchY最终变为0 -touchY // 防止mTouchY最终变为0
} }
} else { } else {
dx = if (mCornerX > 0 && mDirection == Direction.NEXT) { dx = if (mCornerX > 0 && mDirection == PageDirection.NEXT) {
-(viewWidth + touchX) -(viewWidth + touchX)
} else { } else {
(viewWidth - touchX + viewWidth) (viewWidth - touchX + viewWidth)
@ -197,14 +198,14 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
if (!isRunning) return if (!isRunning) return
when (mDirection) { when (mDirection) {
Direction.NEXT -> { PageDirection.NEXT -> {
calcPoints() calcPoints()
drawCurrentPageArea(canvas, curBitmap) drawCurrentPageArea(canvas, curBitmap)
drawNextPageAreaAndShadow(canvas, nextBitmap) drawNextPageAreaAndShadow(canvas, nextBitmap)
drawCurrentPageShadow(canvas) drawCurrentPageShadow(canvas)
drawCurrentBackArea(canvas, curBitmap) drawCurrentBackArea(canvas, curBitmap)
} }
Direction.PREV -> { PageDirection.PREV -> {
calcPoints() calcPoints()
drawCurrentPageArea(canvas, prevBitmap) drawCurrentPageArea(canvas, prevBitmap)
drawNextPageAreaAndShadow(canvas, curBitmap) drawNextPageAreaAndShadow(canvas, curBitmap)

@ -3,6 +3,7 @@ package io.legado.app.ui.book.read.page.delegate
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Matrix import android.graphics.Matrix
import io.legado.app.ui.book.read.page.PageView import io.legado.app.ui.book.read.page.PageView
import io.legado.app.ui.book.read.page.entities.PageDirection
class SlidePageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) { class SlidePageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
@ -11,7 +12,7 @@ class SlidePageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
override fun onAnimStart(animationSpeed: Int) { override fun onAnimStart(animationSpeed: Int) {
val distanceX: Float val distanceX: Float
when (mDirection) { when (mDirection) {
Direction.NEXT -> distanceX = PageDirection.NEXT -> distanceX =
if (isCancel) { if (isCancel) {
var dis = viewWidth - startX + touchX var dis = viewWidth - startX + touchX
if (dis > viewWidth) { if (dis > viewWidth) {
@ -34,17 +35,17 @@ 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 ((mDirection == Direction.NEXT && offsetX > 0) if ((mDirection == PageDirection.NEXT && offsetX > 0)
|| (mDirection == Direction.PREV && offsetX < 0) || (mDirection == PageDirection.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
if (!isRunning) return if (!isRunning) return
if (mDirection == Direction.PREV) { if (mDirection == PageDirection.PREV) {
bitmapMatrix.setTranslate(distanceX + viewWidth, 0.toFloat()) bitmapMatrix.setTranslate(distanceX + viewWidth, 0.toFloat())
curBitmap?.let { canvas.drawBitmap(it, bitmapMatrix, null) } curBitmap?.let { canvas.drawBitmap(it, bitmapMatrix, null) }
bitmapMatrix.setTranslate(distanceX, 0.toFloat()) bitmapMatrix.setTranslate(distanceX, 0.toFloat())
prevBitmap?.let { canvas.drawBitmap(it, bitmapMatrix, null) } prevBitmap?.let { canvas.drawBitmap(it, bitmapMatrix, null) }
} else if (mDirection == Direction.NEXT) { } else if (mDirection == PageDirection.NEXT) {
bitmapMatrix.setTranslate(distanceX, 0.toFloat()) bitmapMatrix.setTranslate(distanceX, 0.toFloat())
nextBitmap?.let { canvas.drawBitmap(it, bitmapMatrix, null) } nextBitmap?.let { canvas.drawBitmap(it, bitmapMatrix, null) }
bitmapMatrix.setTranslate(distanceX - viewWidth, 0.toFloat()) bitmapMatrix.setTranslate(distanceX - viewWidth, 0.toFloat())

@ -0,0 +1,5 @@
package io.legado.app.ui.book.read.page.entities
enum class PageDirection {
NONE, PREV, NEXT
}
Loading…
Cancel
Save