Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Oct 26, 2023
1 parent 0ebf6d6 commit 7deff96
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
50 changes: 22 additions & 28 deletions app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ class ReadBookActivity : BaseReadBookActivity(),
}
}
private var menu: Menu? = null
private var autoPageJob: Job? = null
private var backupJob: Job? = null
private var keepScreenJon: Job? = null
private var tts: TTS? = null
Expand Down Expand Up @@ -173,6 +172,7 @@ class ReadBookActivity : BaseReadBookActivity(),
private var bookChanged = false
private var pageChanged = false
private var reloadContent = false
private val autoPageRenderer by lazy { SyncedRenderer { doAutoPage(it) } }

//恢复跳转前进度对话框的交互结果
private var confirmRestoreProcess: Boolean? = null
Expand Down Expand Up @@ -961,41 +961,35 @@ class ReadBookActivity : BaseReadBookActivity(),
override fun autoPageStop() {
if (isAutoPage) {
isAutoPage = false
autoPageJob?.cancel()
autoPageRenderer.stop()
binding.readView.invalidate()
binding.readMenu.setAutoPage(false)
upScreenTimeOut()
}
}

private fun autoPagePlus() {
autoPageJob?.cancel()
autoPageJob = lifecycleScope.launch {
while (isActive) {
var delayMillis = ReadBookConfig.autoReadSpeed * 1000L / binding.readView.height
var scrollOffset = 1
if (delayMillis < 20) {
var delayInt = delayMillis.toInt()
if (delayInt == 0) delayInt = 1
scrollOffset = 20 / delayInt
delayMillis = 20
}
delay(delayMillis)
if (!menuLayoutIsVisible) {
if (binding.readView.isScroll) {
binding.readView.curPage.scroll(-scrollOffset)
} else {
autoPageProgress += scrollOffset
if (autoPageProgress >= binding.readView.height) {
autoPageProgress = 0
if (!binding.readView.fillPage(PageDirection.NEXT)) {
autoPageStop()
}
} else {
binding.readView.invalidate()
}
}
autoPageRenderer.start()
}

private fun doAutoPage(frameTime: Double) {
if (menuLayoutIsVisible) {
return
}
val readTime = ReadBookConfig.autoReadSpeed * 1000.0
val height = binding.readView.height
val scrollOffset = (height / readTime * frameTime).toInt().coerceAtLeast(1)
if (binding.readView.isScroll) {
binding.readView.curPage.scroll(-scrollOffset)
} else {
autoPageProgress += scrollOffset
if (autoPageProgress >= height) {
autoPageProgress = 0
if (!binding.readView.fillPage(PageDirection.NEXT)) {
autoPageStop()
}
} else {
binding.readView.invalidate()
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/io/legado/app/utils/SyncedRenderer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.legado.app.utils

import android.view.Choreographer

class SyncedRenderer(val doFrame: (frameTime: Double) -> Unit) {

private var callback: (Long) -> Unit = {}

fun start() {
var currTime = System.nanoTime() / 1000000.0
callback = {
val currTimeMs = it / 1000000.0
val frameTime = currTimeMs - currTime
currTime = currTimeMs
doFrame(frameTime)
Choreographer.getInstance().postFrameCallback(callback)
}
Choreographer.getInstance().postFrameCallback(callback)
}

fun stop() {
Choreographer.getInstance().removeFrameCallback(callback)
callback = {}
}
}

0 comments on commit 7deff96

Please sign in to comment.