mirror of
https://github.com/gedoor/legado.git
synced 2024-07-17 00:58:29 +08:00
优化
This commit is contained in:
parent
f83a2bc357
commit
e57ad3a09e
@ -1,18 +1,15 @@
|
||||
package io.legado.app.ui.book.read.page
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Paint
|
||||
import android.graphics.Picture
|
||||
import android.graphics.Rect
|
||||
import android.os.Build
|
||||
import android.os.SystemClock
|
||||
import androidx.core.graphics.withClip
|
||||
import io.legado.app.help.config.AppConfig
|
||||
import io.legado.app.help.config.ReadBookConfig
|
||||
import io.legado.app.lib.theme.ThemeStore
|
||||
import io.legado.app.ui.book.read.page.entities.PageDirection
|
||||
import io.legado.app.utils.screenshot
|
||||
import io.legado.app.utils.canvasrecorder.CanvasRecorderFactory
|
||||
import io.legado.app.utils.canvasrecorder.recordIfNeeded
|
||||
|
||||
/**
|
||||
* 自动翻页
|
||||
@ -24,13 +21,10 @@ class AutoPager(private val readView: ReadView) {
|
||||
private var scrollOffsetRemain = 0.0
|
||||
private var scrollOffset = 0
|
||||
private var lastTimeMillis = 0L
|
||||
private var bitmap: Bitmap? = null
|
||||
private var picture: Picture? = null
|
||||
private var pictureIsDirty = true
|
||||
private val atLeastApi23 = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
|
||||
private val rect = Rect()
|
||||
private var canvasRecorder = CanvasRecorderFactory.create()
|
||||
private val paint by lazy { Paint() }
|
||||
|
||||
|
||||
fun start() {
|
||||
isRunning = true
|
||||
paint.color = ThemeStore.accentColor
|
||||
@ -48,7 +42,7 @@ class AutoPager(private val readView: ReadView) {
|
||||
readView.curPage.upSelectAble(AppConfig.textSelectAble)
|
||||
readView.invalidate()
|
||||
reset()
|
||||
picture = null
|
||||
canvasRecorder.recycle()
|
||||
}
|
||||
|
||||
fun pause() {
|
||||
@ -71,9 +65,12 @@ class AutoPager(private val readView: ReadView) {
|
||||
progress = 0
|
||||
scrollOffsetRemain = 0.0
|
||||
scrollOffset = 0
|
||||
bitmap?.recycle()
|
||||
bitmap = null
|
||||
pictureIsDirty = true
|
||||
canvasRecorder.invalidate()
|
||||
}
|
||||
|
||||
fun upRecorder() {
|
||||
canvasRecorder.recycle()
|
||||
canvasRecorder = CanvasRecorderFactory.create()
|
||||
}
|
||||
|
||||
fun onDraw(canvas: Canvas) {
|
||||
@ -86,24 +83,12 @@ class AutoPager(private val readView: ReadView) {
|
||||
} else {
|
||||
val bottom = progress
|
||||
val width = readView.width
|
||||
if (atLeastApi23) {
|
||||
if (picture == null) {
|
||||
picture = Picture()
|
||||
}
|
||||
if (pictureIsDirty) {
|
||||
pictureIsDirty = false
|
||||
readView.nextPage.screenshot(picture!!)
|
||||
}
|
||||
canvas.withClip(0, 0, width, bottom) {
|
||||
drawPicture(picture!!)
|
||||
}
|
||||
} else {
|
||||
if (bitmap == null) {
|
||||
bitmap = readView.nextPage.screenshot()
|
||||
}
|
||||
rect.set(0, 0, width, bottom)
|
||||
canvas.drawBitmap(bitmap!!, rect, rect, null)
|
||||
|
||||
canvasRecorder.recordIfNeeded(readView.nextPage)
|
||||
canvas.withClip(0, 0, width, bottom) {
|
||||
canvasRecorder.draw(this)
|
||||
}
|
||||
|
||||
canvas.drawRect(
|
||||
0f,
|
||||
bottom.toFloat() - 1,
|
||||
|
@ -514,6 +514,7 @@ class ReadView(context: Context, attrs: AttributeSet) :
|
||||
(pageDelegate as? ScrollPageDelegate)?.noAnim = AppConfig.noAnimScrollPage
|
||||
if (upRecorder) {
|
||||
(pageDelegate as? HorizontalPageDelegate)?.upRecorder()
|
||||
autoPager.upRecorder()
|
||||
}
|
||||
pageDelegate?.setViewSize(width, height)
|
||||
if (isScroll) {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package io.legado.app.utils.canvasrecorder
|
||||
|
||||
import android.graphics.Canvas
|
||||
import android.view.View
|
||||
import androidx.core.graphics.withSave
|
||||
|
||||
inline fun CanvasRecorder.recordIfNeeded(
|
||||
@ -13,6 +14,14 @@ inline fun CanvasRecorder.recordIfNeeded(
|
||||
return true
|
||||
}
|
||||
|
||||
fun CanvasRecorder.recordIfNeeded(view: View): Boolean {
|
||||
if (!needRecord()) return false
|
||||
record(view.width, view.height) {
|
||||
view.draw(this)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
inline fun CanvasRecorder.record(width: Int, height: Int, block: Canvas.() -> Unit) {
|
||||
try {
|
||||
val canvas = beginRecording(width, height)
|
||||
|
Loading…
Reference in New Issue
Block a user