优化
This commit is contained in:
@@ -6,7 +6,6 @@ import android.graphics.Paint
|
|||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import android.view.MotionEvent
|
import android.view.MotionEvent
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.core.graphics.withTranslation
|
|
||||||
import io.legado.app.R
|
import io.legado.app.R
|
||||||
import io.legado.app.data.entities.Bookmark
|
import io.legado.app.data.entities.Bookmark
|
||||||
import io.legado.app.help.config.AppConfig
|
import io.legado.app.help.config.AppConfig
|
||||||
@@ -23,6 +22,7 @@ import io.legado.app.ui.book.read.page.entities.column.TextColumn
|
|||||||
import io.legado.app.ui.book.read.page.provider.ChapterProvider
|
import io.legado.app.ui.book.read.page.provider.ChapterProvider
|
||||||
import io.legado.app.ui.book.read.page.provider.TextPageFactory
|
import io.legado.app.ui.book.read.page.provider.TextPageFactory
|
||||||
import io.legado.app.ui.widget.dialog.PhotoDialog
|
import io.legado.app.ui.widget.dialog.PhotoDialog
|
||||||
|
import io.legado.app.utils.PictureMirror
|
||||||
import io.legado.app.utils.activity
|
import io.legado.app.utils.activity
|
||||||
import io.legado.app.utils.getCompatColor
|
import io.legado.app.utils.getCompatColor
|
||||||
import io.legado.app.utils.showDialogFragment
|
import io.legado.app.utils.showDialogFragment
|
||||||
@@ -102,25 +102,18 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
|
|||||||
*/
|
*/
|
||||||
private fun drawPage(canvas: Canvas) {
|
private fun drawPage(canvas: Canvas) {
|
||||||
var relativeOffset = relativeOffset(0)
|
var relativeOffset = relativeOffset(0)
|
||||||
val view = this
|
textPage.draw(this, canvas, relativeOffset)
|
||||||
canvas.withTranslation(0f, relativeOffset) {
|
|
||||||
textPage.draw(view, this)
|
|
||||||
}
|
|
||||||
if (!callBack.isScroll) return
|
if (!callBack.isScroll) return
|
||||||
//滚动翻页
|
//滚动翻页
|
||||||
if (!pageFactory.hasNext()) return
|
if (!pageFactory.hasNext()) return
|
||||||
val textPage1 = relativePage(1)
|
val textPage1 = relativePage(1)
|
||||||
relativeOffset = relativeOffset(1)
|
relativeOffset = relativeOffset(1)
|
||||||
canvas.withTranslation(0f, relativeOffset) {
|
textPage1.draw(this, canvas, relativeOffset)
|
||||||
textPage1.draw(view, this)
|
|
||||||
}
|
|
||||||
if (!pageFactory.hasNextPlus()) return
|
if (!pageFactory.hasNextPlus()) return
|
||||||
relativeOffset = relativeOffset(2)
|
relativeOffset = relativeOffset(2)
|
||||||
if (relativeOffset < ChapterProvider.visibleHeight) {
|
if (relativeOffset < ChapterProvider.visibleHeight) {
|
||||||
val textPage2 = relativePage(2)
|
val textPage2 = relativePage(2)
|
||||||
canvas.withTranslation(0f, relativeOffset) {
|
textPage2.draw(this, canvas, relativeOffset)
|
||||||
textPage2.draw(view, this)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,7 +160,9 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun submitPreRenderTask() {
|
fun submitPreRenderTask() {
|
||||||
renderThread.submit(renderRunnable)
|
if (PictureMirror.atLeastApi23) {
|
||||||
|
renderThread.submit(renderRunnable)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun preRenderPage() {
|
private fun preRenderPage() {
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ data class TextLine(
|
|||||||
return visible
|
return visible
|
||||||
}
|
}
|
||||||
|
|
||||||
fun draw(view: ContentTextView, canvas: Canvas?) {
|
fun draw(view: ContentTextView, canvas: Canvas) {
|
||||||
pictureMirror.draw(canvas, view.width, height.toInt()) {
|
pictureMirror.draw(canvas, view.width, height.toInt()) {
|
||||||
drawTextLine(view, this)
|
drawTextLine(view, this)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ data class TextPage(
|
|||||||
var isMsgPage: Boolean = false
|
var isMsgPage: Boolean = false
|
||||||
var pictureMirror: PictureMirror = PictureMirror()
|
var pictureMirror: PictureMirror = PictureMirror()
|
||||||
var doublePage = false
|
var doublePage = false
|
||||||
|
var paddingTop = 0
|
||||||
|
|
||||||
val paragraphs by lazy {
|
val paragraphs by lazy {
|
||||||
paragraphsInternal
|
paragraphsInternal
|
||||||
@@ -159,6 +160,7 @@ data class TextPage(
|
|||||||
addLine(textLine)
|
addLine(textLine)
|
||||||
}
|
}
|
||||||
height = ChapterProvider.visibleHeight.toFloat()
|
height = ChapterProvider.visibleHeight.toFloat()
|
||||||
|
invalidate()
|
||||||
}
|
}
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
@@ -180,7 +182,8 @@ data class TextPage(
|
|||||||
fun upPageAloudSpan(aloudSpanStart: Int) {
|
fun upPageAloudSpan(aloudSpanStart: Int) {
|
||||||
removePageAloudSpan()
|
removePageAloudSpan()
|
||||||
var lineStart = 0
|
var lineStart = 0
|
||||||
for ((index, textLine) in textLines.withIndex()) {
|
for (index in textLines.indices) {
|
||||||
|
val textLine = textLines[index]
|
||||||
val lineLength = textLine.text.length + if (textLine.isParagraphEnd) 1 else 0
|
val lineLength = textLine.text.length + if (textLine.isParagraphEnd) 1 else 0
|
||||||
if (aloudSpanStart > lineStart && aloudSpanStart < lineStart + lineLength) {
|
if (aloudSpanStart > lineStart && aloudSpanStart < lineStart + lineLength) {
|
||||||
for (i in index - 1 downTo 0) {
|
for (i in index - 1 downTo 0) {
|
||||||
@@ -263,16 +266,19 @@ data class TextPage(
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
fun draw(view: ContentTextView, canvas: Canvas?) {
|
fun draw(view: ContentTextView, canvas: Canvas, relativeOffset: Float) {
|
||||||
pictureMirror.drawLocked(canvas, view.width, height.toInt()) {
|
val height = height.toInt()
|
||||||
drawPage(view, this)
|
canvas.withTranslation(0f, relativeOffset + paddingTop) {
|
||||||
|
pictureMirror.drawLocked(canvas, view.width, height) {
|
||||||
|
drawPage(view, this)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun drawPage(view: ContentTextView, canvas: Canvas) {
|
private fun drawPage(view: ContentTextView, canvas: Canvas) {
|
||||||
for (i in lines.indices) {
|
for (i in lines.indices) {
|
||||||
val line = lines[i]
|
val line = lines[i]
|
||||||
canvas.withTranslation(0f, line.lineTop) {
|
canvas.withTranslation(0f, line.lineTop - paddingTop) {
|
||||||
line.draw(view, this)
|
line.draw(view, this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -280,7 +286,9 @@ data class TextPage(
|
|||||||
|
|
||||||
fun preRender(view: ContentTextView): Boolean {
|
fun preRender(view: ContentTextView): Boolean {
|
||||||
if (!pictureMirror.isDirty) return false
|
if (!pictureMirror.isDirty) return false
|
||||||
draw(view, null)
|
pictureMirror.drawLocked(null, view.width, height.toInt()) {
|
||||||
|
drawPage(view, this)
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -290,6 +290,7 @@ object ChapterProvider {
|
|||||||
item.chapterSize = chapterSize
|
item.chapterSize = chapterSize
|
||||||
item.title = displayTitle
|
item.title = displayTitle
|
||||||
item.doublePage = doublePage
|
item.doublePage = doublePage
|
||||||
|
item.paddingTop = paddingTop
|
||||||
item.upLinesPosition()
|
item.upLinesPosition()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun hasNext(): Boolean = with(dataSource) {
|
override fun hasNext(): Boolean = with(dataSource) {
|
||||||
return hasNextChapter() || currentChapter?.isLastIndex(pageIndex) != true
|
return hasNextChapter() || (currentChapter != null && currentChapter?.isLastIndex(pageIndex) != true)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun hasNextPlus(): Boolean = with(dataSource) {
|
override fun hasNextPlus(): Boolean = with(dataSource) {
|
||||||
@@ -85,14 +85,12 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
|
|||||||
return@with TextPage(text = it).format()
|
return@with TextPage(text = it).format()
|
||||||
}
|
}
|
||||||
currentChapter?.let {
|
currentChapter?.let {
|
||||||
|
val pageIndex = pageIndex
|
||||||
if (pageIndex < it.pageSize - 1) {
|
if (pageIndex < it.pageSize - 1) {
|
||||||
return@with it.getPage(pageIndex + 1)?.removePageAloudSpan()
|
return@with it.getPage(pageIndex + 1)?.removePageAloudSpan()
|
||||||
?: TextPage(title = it.title).format()
|
?: TextPage(title = it.title).format()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!hasNextChapter()) {
|
|
||||||
return@with TextPage(text = "")
|
|
||||||
}
|
|
||||||
nextChapter?.let {
|
nextChapter?.let {
|
||||||
return@with it.getPage(0)?.removePageAloudSpan()
|
return@with it.getPage(0)?.removePageAloudSpan()
|
||||||
?: TextPage(title = it.title).format()
|
?: TextPage(title = it.title).format()
|
||||||
@@ -105,8 +103,9 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
|
|||||||
ReadBook.msg?.let {
|
ReadBook.msg?.let {
|
||||||
return@with TextPage(text = it).format()
|
return@with TextPage(text = it).format()
|
||||||
}
|
}
|
||||||
if (pageIndex > 0) {
|
currentChapter?.let {
|
||||||
currentChapter?.let {
|
val pageIndex = pageIndex
|
||||||
|
if (pageIndex > 0) {
|
||||||
return@with it.getPage(pageIndex - 1)?.removePageAloudSpan()
|
return@with it.getPage(pageIndex - 1)?.removePageAloudSpan()
|
||||||
?: TextPage(title = it.title).format()
|
?: TextPage(title = it.title).format()
|
||||||
}
|
}
|
||||||
@@ -121,6 +120,7 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
|
|||||||
override val nextPlusPage: TextPage
|
override val nextPlusPage: TextPage
|
||||||
get() = with(dataSource) {
|
get() = with(dataSource) {
|
||||||
currentChapter?.let {
|
currentChapter?.let {
|
||||||
|
val pageIndex = pageIndex
|
||||||
if (pageIndex < it.pageSize - 2) {
|
if (pageIndex < it.pageSize - 2) {
|
||||||
return@with it.getPage(pageIndex + 2)?.removePageAloudSpan()
|
return@with it.getPage(pageIndex + 2)?.removePageAloudSpan()
|
||||||
?: TextPage(title = it.title).format()
|
?: TextPage(title = it.title).format()
|
||||||
@@ -133,7 +133,6 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
|
|||||||
return@with nc.getPage(1)?.removePageAloudSpan()
|
return@with nc.getPage(1)?.removePageAloudSpan()
|
||||||
?: TextPage(text = "继续滑动以加载下一章…").format()
|
?: TextPage(text = "继续滑动以加载下一章…").format()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return TextPage().format()
|
return TextPage().format()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,15 +23,11 @@ class PictureMirror {
|
|||||||
block: Canvas.() -> Unit
|
block: Canvas.() -> Unit
|
||||||
) {
|
) {
|
||||||
if (atLeastApi23) {
|
if (atLeastApi23) {
|
||||||
if (picture == null) {
|
if (picture == null || lock == null) {
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
if (picture == null) {
|
if (picture == null) {
|
||||||
picture = Picture()
|
picture = Picture()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
if (lock == null) {
|
|
||||||
synchronized(this) {
|
|
||||||
if (lock == null) {
|
if (lock == null) {
|
||||||
lock = ReentrantLock()
|
lock = ReentrantLock()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user