feat: 优化代码

pull/134/head^2
kunfei 5 years ago
parent b7bae95675
commit 8cce205cbb
  1. 12
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt
  2. 10
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt

@ -104,11 +104,7 @@ object ChapterProvider {
Layout.Alignment.ALIGN_NORMAL, 0f, 0f, true Layout.Alignment.ALIGN_NORMAL, 0f, 0f, true
) )
for (lineIndex in 0 until layout.lineCount) { for (lineIndex in 0 until layout.lineCount) {
textPages.last().height = durY
val textLine = TextLine(isTitle = isTitle) val textLine = TextLine(isTitle = isTitle)
textLine.lineBottom = paddingTop + durY + textPaint.textHeight
textLine.lineBase = textLine.lineBottom - textPaint.fontMetrics.descent
textLine.lineTop = paddingTop + durY
val words = val words =
text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex)) text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex))
stringBuilder.append(words) stringBuilder.append(words)
@ -128,17 +124,23 @@ object ChapterProvider {
addCharsToLineMiddle(textLine, words, textPaint, desiredWidth, 0f) addCharsToLineMiddle(textLine, words, textPaint, desiredWidth, 0f)
} }
if (durY + textPaint.textHeight < visibleHeight) { if (durY + textPaint.textHeight < visibleHeight) {
textLine.upTopBottom(durY, textPaint)
textPages.last().textLines.add(textLine) textPages.last().textLines.add(textLine)
durY += textPaint.textHeight * lineSpacingExtra / 10f durY += textPaint.textHeight * lineSpacingExtra / 10f
textPages.last().height = durY
} else { } else {
textPages.last().text = stringBuilder.toString() textPages.last().text = stringBuilder.toString()
stringBuilder.clear() stringBuilder.clear()
pageLines.add(textPages.last().textLines.size) pageLines.add(textPages.last().textLines.size)
pageLengths.add(textPages.last().text.length) pageLengths.add(textPages.last().text.length)
textPages.last().height = durY
//新建页面 //新建页面
durY = 0f
textLine.upTopBottom(durY, textPaint)
durY = textPaint.textHeight * lineSpacingExtra / 10f durY = textPaint.textHeight * lineSpacingExtra / 10f
textPages.add(TextPage()) textPages.add(TextPage())
textPages.last().textLines.add(textLine) textPages.last().textLines.add(textLine)
textPages.last().height = durY
} }
} }
durY += textPaint.textHeight * paragraphSpacing / 10f durY += textPaint.textHeight * paragraphSpacing / 10f
@ -290,7 +292,7 @@ object ChapterProvider {
visibleBottom = paddingTop + visibleHeight visibleBottom = paddingTop + visibleHeight
} }
private val TextPaint.textHeight: Float val TextPaint.textHeight: Float
get() { get() {
return this.fontMetrics.descent - fontMetrics.ascent + fontMetrics.leading return this.fontMetrics.descent - fontMetrics.ascent + fontMetrics.leading
} }

@ -1,5 +1,9 @@
package io.legado.app.ui.book.read.page.entities package io.legado.app.ui.book.read.page.entities
import android.text.TextPaint
import io.legado.app.ui.book.read.page.ChapterProvider
import io.legado.app.ui.book.read.page.ChapterProvider.textHeight
data class TextLine( data class TextLine(
var text: String = "", var text: String = "",
val textChars: ArrayList<TextChar> = arrayListOf(), val textChars: ArrayList<TextChar> = arrayListOf(),
@ -10,6 +14,12 @@ data class TextLine(
var isReadAloud: Boolean = false var isReadAloud: Boolean = false
) { ) {
fun upTopBottom(durY: Float, textPaint: TextPaint) {
lineTop = ChapterProvider.paddingTop + durY
lineBottom = lineTop + textPaint.textHeight
lineBase = lineBottom - textPaint.fontMetrics.descent
}
fun addTextChar(charData: String, start: Float, end: Float) { fun addTextChar(charData: String, start: Float, end: Float) {
textChars.add(TextChar(charData, start = start, end = end)) textChars.add(TextChar(charData, start = start, end = end))
} }

Loading…
Cancel
Save