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