feat: 优化代码

pull/106/head
kunfei 5 years ago
parent c255d6c6f1
commit 27d40c5cdf
  1. 45
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt

@ -88,6 +88,7 @@ object ChapterProvider {
val textPages = arrayListOf<TextPage>() val textPages = arrayListOf<TextPage>()
val pageLines = arrayListOf<Int>() val pageLines = arrayListOf<Int>()
val pageLengths = arrayListOf<Int>() val pageLengths = arrayListOf<Int>()
val stringBuilder = StringBuilder()
var surplusText = content var surplusText = content
var durY = 0 var durY = 0
textPages.add(TextPage()) textPages.add(TextPage())
@ -98,7 +99,7 @@ object ChapterProvider {
if (end > 0) { if (end > 0) {
val title = surplusText.substring(0, end) val title = surplusText.substring(0, end)
surplusText = surplusText.substring(end + 1) surplusText = surplusText.substring(end + 1)
durY = joinTitle(title, durY, textPages) durY = joinTitle(title, durY, textPages, pageLines, pageLengths, stringBuilder)
} }
} else { } else {
//正文 //正文
@ -111,9 +112,15 @@ object ChapterProvider {
text = surplusText text = surplusText
surplusText = "" surplusText = ""
} }
durY = joinBody(text, durY, textPages) durY = joinBody(text, durY, textPages, pageLines, pageLengths, stringBuilder)
} }
} }
if (pageLines.size < textPages.size) {
pageLines.add(textPages.last().textLines.size)
}
if (pageLengths.size < textPages.size) {
pageLengths.add(textPages.last().text.length)
}
for ((index, item) in textPages.withIndex()) { for ((index, item) in textPages.withIndex()) {
item.index = index item.index = index
item.pageSize = textPages.size item.pageSize = textPages.size
@ -132,7 +139,14 @@ object ChapterProvider {
) )
} }
private fun joinTitle(title: String, y: Int, textPages: ArrayList<TextPage>): Int { private fun joinTitle(
title: String,
y: Int,
textPages: ArrayList<TextPage>,
pageLines: ArrayList<Int>,
pageLengths: ArrayList<Int>,
stringBuilder: StringBuilder
): Int {
var durY = y var durY = y
val layout = StaticLayout( val layout = StaticLayout(
title, titlePaint, visibleWidth, title, titlePaint, visibleWidth,
@ -145,6 +159,10 @@ object ChapterProvider {
textPages.last().textLines.add(textLine) textPages.last().textLines.add(textLine)
} else { } else {
durY = layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex) durY = layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex)
textPages.last().text = stringBuilder.toString()
stringBuilder.clear()
pageLines.add(textPages.last().textLines.size)
pageLengths.add(textPages.last().text.length)
textPages.add(TextPage()) textPages.add(TextPage())
textPages.last().textLines.add(textLine) textPages.last().textLines.add(textLine)
} }
@ -154,10 +172,9 @@ object ChapterProvider {
) )
textLine.lineTop = textLine.lineTop =
paddingTop + durY - layout.getLineBottom(lineIndex) + layout.getLineTop(lineIndex) paddingTop + durY - layout.getLineBottom(lineIndex) + layout.getLineTop(lineIndex)
val words = title.substring( val words =
layout.getLineStart(lineIndex), title.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex))
layout.getLineEnd(lineIndex) stringBuilder.append(words)
)
val desiredWidth = layout.getLineMax(lineIndex) val desiredWidth = layout.getLineMax(lineIndex)
if (lineIndex != layout.lineCount - 1) { if (lineIndex != layout.lineCount - 1) {
val gapCount: Int = words.length - 1 val gapCount: Int = words.length - 1
@ -181,6 +198,7 @@ object ChapterProvider {
} }
} else { } else {
//最后一行 //最后一行
stringBuilder.append("\n")
var x = 0 var x = 0
for (i in words.indices) { for (i in words.indices) {
val char = words[i].toString() val char = words[i].toString()
@ -200,7 +218,14 @@ object ChapterProvider {
return durY return durY
} }
private fun joinBody(text: String, y: Int, textPages: ArrayList<TextPage>): Int { private fun joinBody(
text: String,
y: Int,
textPages: ArrayList<TextPage>,
pageLines: ArrayList<Int>,
pageLengths: ArrayList<Int>,
stringBuilder: StringBuilder
): Int {
var durY = y var durY = y
val layout = StaticLayout( val layout = StaticLayout(
text, contentPaint, visibleWidth, text, contentPaint, visibleWidth,
@ -213,6 +238,10 @@ object ChapterProvider {
textPages.last().textLines.add(textLine) textPages.last().textLines.add(textLine)
} else { } else {
durY = layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex) durY = layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex)
textPages.last().text = stringBuilder.toString()
stringBuilder.clear()
pageLines.add(textPages.last().textLines.size)
pageLengths.add(textPages.last().text.length)
textPages.add(TextPage()) textPages.add(TextPage())
textPages.last().textLines.add(textLine) textPages.last().textLines.add(textLine)
} }

@ -54,7 +54,7 @@ data class TextChapter(
fun getReadLength(pageIndex: Int): Int { fun getReadLength(pageIndex: Int): Int {
var length = 0 var length = 0
val maxIndex = min(pageIndex, pageLines.size) val maxIndex = min(pageIndex, pages.size)
for (index in 0 until maxIndex) { for (index in 0 until maxIndex) {
length += pageLengths[index] length += pageLengths[index]
} }

Loading…
Cancel
Save