feat: 优化代码

pull/133/head
kunfei 5 years ago
parent d57d511053
commit 5d2d738959
  1. 5
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt
  2. 33
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/CoverPageDelegate.kt
  3. 8
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt
  4. 17
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt
  5. 3
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/SimulationPageDelegate.kt
  6. 27
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/SlidePageDelegate.kt

@ -26,12 +26,12 @@ class PageView(context: Context, attrs: AttributeSet) :
init { init {
callBack = activity as CallBack callBack = activity as CallBack
prevPage = ContentView(context)
addView(prevPage)
nextPage = ContentView(context) nextPage = ContentView(context)
addView(nextPage) addView(nextPage)
curPage = ContentView(context) curPage = ContentView(context)
addView(curPage) addView(curPage)
prevPage = ContentView(context)
addView(prevPage)
upBg() upBg()
setWillNotDraw(false) setWillNotDraw(false)
pageFactory = TextPageFactory(this) pageFactory = TextPageFactory(this)
@ -40,6 +40,7 @@ class PageView(context: Context, attrs: AttributeSet) :
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh) super.onSizeChanged(w, h, oldw, oldh)
prevPage.x = -w.toFloat()
pageDelegate?.setViewSize(w, h) pageDelegate?.setViewSize(w, h)
if (oldw != 0 && oldh != 0) { if (oldw != 0 && oldh != 0) {
ReadBook.loadContent() ReadBook.loadContent()

@ -1,14 +1,12 @@
package io.legado.app.ui.book.read.page.delegate package io.legado.app.ui.book.read.page.delegate
import android.graphics.Canvas import android.graphics.Canvas
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
class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) { class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
private val shadowDrawableR: GradientDrawable private val shadowDrawableR: GradientDrawable
private val bitmapMatrix = Matrix()
init { init {
val shadowColors = intArrayOf(0x66111111, 0x00000000) val shadowColors = intArrayOf(0x66111111, 0x00000000)
@ -38,17 +36,9 @@ class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
viewWidth - (touchX - startX) viewWidth - (touchX - startX)
} }
} }
startScroll(touchX.toInt(), 0, distanceX.toInt(), 0) startScroll(touchX.toInt(), 0, distanceX.toInt(), 0)
} }
override fun onAnimStop() {
curPage.x = 0.toFloat()
if (!isCancel) {
pageView.fillPage(mDirection)
}
}
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
val offsetX = touchX - startX val offsetX = touchX - startX
@ -57,15 +47,13 @@ class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
) return ) return
val distanceX = if (offsetX > 0) offsetX - viewWidth else offsetX + viewWidth val distanceX = if (offsetX > 0) offsetX - viewWidth else offsetX + viewWidth
bitmap?.let { if (!isMoved) return
if (mDirection == Direction.PREV) { if (mDirection == Direction.PREV) {
bitmapMatrix.setTranslate(distanceX, 0.toFloat()) prevPage.translationX = offsetX - viewWidth
canvas.drawBitmap(it, bitmapMatrix, null) } else if (mDirection == Direction.NEXT) {
} else if (mDirection == Direction.NEXT) { curPage.translationX = offsetX
curPage.translationX = offsetX
}
addShadow(distanceX.toInt(), canvas)
} }
addShadow(distanceX.toInt(), canvas)
} }
private fun addShadow(left: Int, canvas: Canvas) { private fun addShadow(left: Int, canvas: Canvas) {
@ -77,4 +65,13 @@ class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
shadowDrawableR.draw(canvas) shadowDrawableR.draw(canvas)
} }
} }
override fun onAnimStop() {
curPage.x = 0.toFloat()
prevPage.x = -viewWidth.toFloat()
if (!isCancel) {
pageView.fillPage(mDirection)
}
}
} }

@ -8,6 +8,9 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
override fun onTouch(event: MotionEvent) { override fun onTouch(event: MotionEvent) {
when (event.action) { when (event.action) {
MotionEvent.ACTION_DOWN -> {
abort()
}
MotionEvent.ACTION_MOVE -> { MotionEvent.ACTION_MOVE -> {
if (isTextSelected) { if (isTextSelected) {
selectText(event) selectText(event)
@ -32,7 +35,6 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
return return
} }
setDirection(Direction.PREV) setDirection(Direction.PREV)
setBitmap()
} else { } else {
//如果不存在表示没有下一页了 //如果不存在表示没有下一页了
if (!hasNext()) { if (!hasNext()) {
@ -40,7 +42,6 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
return return
} }
setDirection(Direction.NEXT) setDirection(Direction.NEXT)
setBitmap()
} }
} }
} }
@ -55,7 +56,6 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
override fun nextPageByAnim() { override fun nextPageByAnim() {
if (!hasNext()) return if (!hasNext()) return
setDirection(Direction.NEXT) setDirection(Direction.NEXT)
setBitmap()
setTouchPoint(viewWidth.toFloat(), 0f) setTouchPoint(viewWidth.toFloat(), 0f)
onAnimStart() onAnimStart()
} }
@ -63,8 +63,8 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
override fun prevPageByAnim() { override fun prevPageByAnim() {
if (!hasPrev()) return if (!hasPrev()) return
setDirection(Direction.PREV) setDirection(Direction.PREV)
setBitmap()
setTouchPoint(0f, 0f) setTouchPoint(0f, 0f)
onAnimStart() onAnimStart()
} }
} }

@ -1,7 +1,6 @@
package io.legado.app.ui.book.read.page.delegate package io.legado.app.ui.book.read.page.delegate
import android.content.Context import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.RectF import android.graphics.RectF
import android.view.GestureDetector import android.view.GestureDetector
@ -15,7 +14,6 @@ import io.legado.app.help.AppConfig
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
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.utils.screenshot
import kotlin.math.abs import kotlin.math.abs
abstract class PageDelegate(protected val pageView: PageView) : abstract class PageDelegate(protected val pageView: PageView) :
@ -40,8 +38,6 @@ abstract class PageDelegate(protected val pageView: PageView) :
protected val curPage: ContentView get() = pageView.curPage protected val curPage: ContentView get() = pageView.curPage
protected val prevPage: ContentView get() = pageView.prevPage protected val prevPage: ContentView get() = pageView.prevPage
protected var bitmap: Bitmap? = null
protected var viewWidth: Int = pageView.width protected var viewWidth: Int = pageView.width
protected var viewHeight: Int = pageView.height protected var viewHeight: Int = pageView.height
@ -126,11 +122,10 @@ abstract class PageDelegate(protected val pageView: PageView) :
} }
private fun stopScroll() { private fun stopScroll() {
isMoved = false
isRunning = false isRunning = false
isStarted = false isStarted = false
pageView.invalidate() pageView.invalidate()
bitmap?.recycle()
bitmap = null
} }
open fun setViewSize(width: Int, height: Int) { open fun setViewSize(width: Int, height: Int) {
@ -175,14 +170,6 @@ abstract class PageDelegate(protected val pageView: PageView) :
mDirection = direction mDirection = direction
} }
open fun setBitmap() {
bitmap = when (mDirection) {
Direction.NEXT -> nextPage.screenshot()
Direction.PREV -> prevPage.screenshot()
else -> null
}
}
/** /**
* 触摸事件处理 * 触摸事件处理
*/ */
@ -321,7 +308,7 @@ abstract class PageDelegate(protected val pageView: PageView) :
} }
open fun onDestroy() { open fun onDestroy() {
bitmap?.recycle()
} }
enum class Direction { enum class Direction {

@ -142,6 +142,7 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
override fun setDirection(direction: Direction) { override fun setDirection(direction: Direction) {
super.setDirection(direction) super.setDirection(direction)
setBitmap()
when (direction) { when (direction) {
Direction.PREV -> Direction.PREV ->
//上一页滑动不出现对角 //上一页滑动不出现对角
@ -158,7 +159,7 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
} }
} }
override fun setBitmap() { fun setBitmap() {
when (mDirection) { when (mDirection) {
Direction.PREV -> { Direction.PREV -> {
prevBitmap = prevPage.screenshot() prevBitmap = prevPage.screenshot()

@ -1,13 +1,10 @@
package io.legado.app.ui.book.read.page.delegate package io.legado.app.ui.book.read.page.delegate
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Matrix
import io.legado.app.ui.book.read.page.PageView import io.legado.app.ui.book.read.page.PageView
class SlidePageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) { class SlidePageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
private val bitmapMatrix = Matrix()
override fun onAnimStart() { override fun onAnimStart() {
val distanceX: Float val distanceX: Float
when (mDirection) { when (mDirection) {
@ -28,7 +25,6 @@ class SlidePageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
viewWidth - (touchX - startX) viewWidth - (touchX - startX)
} }
} }
startScroll(touchX.toInt(), 0, distanceX.toInt(), 0) startScroll(touchX.toInt(), 0, distanceX.toInt(), 0)
} }
@ -39,26 +35,19 @@ class SlidePageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
|| (mDirection == Direction.PREV && offsetX < 0) || (mDirection == Direction.PREV && offsetX < 0)
) return ) return
val distanceX = if (offsetX > 0) offsetX - viewWidth else offsetX + viewWidth if (!isMoved) return
bitmap?.let { if (mDirection == Direction.PREV) {
bitmapMatrix.setTranslate(distanceX, 0.toFloat()) prevPage.translationX = offsetX - viewWidth
canvas.drawBitmap(it, bitmapMatrix, null) curPage.translationX = offsetX
} else if (mDirection == Direction.NEXT) {
curPage.translationX = offsetX
nextPage.translationX = curPage.x + viewWidth
} }
} }
override fun onScroll() {
val offsetX = touchX - startX
if ((mDirection == Direction.NEXT && offsetX > 0)
|| (mDirection == Direction.PREV && offsetX < 0)
) return
curPage.translationX = offsetX
}
override fun onAnimStop() { override fun onAnimStop() {
curPage.x = 0.toFloat() curPage.x = 0.toFloat()
prevPage.x = -viewWidth.toFloat()
if (!isCancel) { if (!isCancel) {
pageView.fillPage(mDirection) pageView.fillPage(mDirection)
} }

Loading…
Cancel
Save