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

@ -1,14 +1,12 @@
package io.legado.app.ui.book.read.page.delegate
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.drawable.GradientDrawable
import io.legado.app.ui.book.read.page.PageView
class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
private val shadowDrawableR: GradientDrawable
private val bitmapMatrix = Matrix()
init {
val shadowColors = intArrayOf(0x66111111, 0x00000000)
@ -38,17 +36,9 @@ class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
viewWidth - (touchX - startX)
}
}
startScroll(touchX.toInt(), 0, distanceX.toInt(), 0)
}
override fun onAnimStop() {
curPage.x = 0.toFloat()
if (!isCancel) {
pageView.fillPage(mDirection)
}
}
override fun onDraw(canvas: Canvas) {
val offsetX = touchX - startX
@ -57,16 +47,14 @@ class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
) return
val distanceX = if (offsetX > 0) offsetX - viewWidth else offsetX + viewWidth
bitmap?.let {
if (!isMoved) return
if (mDirection == Direction.PREV) {
bitmapMatrix.setTranslate(distanceX, 0.toFloat())
canvas.drawBitmap(it, bitmapMatrix, null)
prevPage.translationX = offsetX - viewWidth
} else if (mDirection == Direction.NEXT) {
curPage.translationX = offsetX
}
addShadow(distanceX.toInt(), canvas)
}
}
private fun addShadow(left: Int, canvas: Canvas) {
if (left < 0) {
@ -77,4 +65,13 @@ class CoverPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageView) {
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) {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
abort()
}
MotionEvent.ACTION_MOVE -> {
if (isTextSelected) {
selectText(event)
@ -32,7 +35,6 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
return
}
setDirection(Direction.PREV)
setBitmap()
} else {
//如果不存在表示没有下一页了
if (!hasNext()) {
@ -40,7 +42,6 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
return
}
setDirection(Direction.NEXT)
setBitmap()
}
}
}
@ -55,7 +56,6 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
override fun nextPageByAnim() {
if (!hasNext()) return
setDirection(Direction.NEXT)
setBitmap()
setTouchPoint(viewWidth.toFloat(), 0f)
onAnimStart()
}
@ -63,8 +63,8 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
override fun prevPageByAnim() {
if (!hasPrev()) return
setDirection(Direction.PREV)
setBitmap()
setTouchPoint(0f, 0f)
onAnimStart()
}
}

@ -1,7 +1,6 @@
package io.legado.app.ui.book.read.page.delegate
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.RectF
import android.view.GestureDetector
@ -15,7 +14,6 @@ import io.legado.app.help.AppConfig
import io.legado.app.help.ReadBookConfig
import io.legado.app.ui.book.read.page.ContentView
import io.legado.app.ui.book.read.page.PageView
import io.legado.app.utils.screenshot
import kotlin.math.abs
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 prevPage: ContentView get() = pageView.prevPage
protected var bitmap: Bitmap? = null
protected var viewWidth: Int = pageView.width
protected var viewHeight: Int = pageView.height
@ -126,11 +122,10 @@ abstract class PageDelegate(protected val pageView: PageView) :
}
private fun stopScroll() {
isMoved = false
isRunning = false
isStarted = false
pageView.invalidate()
bitmap?.recycle()
bitmap = null
}
open fun setViewSize(width: Int, height: Int) {
@ -175,14 +170,6 @@ abstract class PageDelegate(protected val pageView: PageView) :
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() {
bitmap?.recycle()
}
enum class Direction {

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

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

Loading…
Cancel
Save