feat: 优化代码

pull/133/head
kunfei 5 years ago
parent 939b7a7779
commit d578e0e958
  1. 48
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt
  2. 4
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt

@ -28,7 +28,7 @@ object ChapterProvider {
var visibleHeight = 0 var visibleHeight = 0
var visibleRight = 0 var visibleRight = 0
var visibleBottom = 0 var visibleBottom = 0
private var lineSpacingExtra = 0f private var lineSpacingExtra = 0
private var paragraphSpacing = 0 private var paragraphSpacing = 0
var typeface: Typeface = Typeface.SANS_SERIF var typeface: Typeface = Typeface.SANS_SERIF
var titlePaint = TextPaint() var titlePaint = TextPaint()
@ -51,7 +51,7 @@ object ChapterProvider {
val pageLengths = arrayListOf<Int>() val pageLengths = arrayListOf<Int>()
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
var surplusText = content var surplusText = content
var durY = 0 var durY = 0f
textPages.add(TextPage()) textPages.add(TextPage())
while (surplusText.isNotEmpty()) { while (surplusText.isNotEmpty()) {
if (textPages.first().textLines.isEmpty()) { if (textPages.first().textLines.isEmpty()) {
@ -107,40 +107,37 @@ object ChapterProvider {
*/ */
private fun joinTitle( private fun joinTitle(
title: String, title: String,
y: Int, y: Float,
textPages: ArrayList<TextPage>, textPages: ArrayList<TextPage>,
pageLines: ArrayList<Int>, pageLines: ArrayList<Int>,
pageLengths: ArrayList<Int>, pageLengths: ArrayList<Int>,
stringBuilder: StringBuilder stringBuilder: StringBuilder
): Int { ): Float {
var durY = y var durY = y
val layout = StaticLayout( val layout = StaticLayout(
title, titlePaint, visibleWidth, title, titlePaint, visibleWidth,
Layout.Alignment.ALIGN_NORMAL, 1f, lineSpacingExtra, 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 textPages.last().height = durY
val textLine = TextLine(isTitle = true) val textLine = TextLine(isTitle = true)
if (durY + layout.getLineBaseline(lineIndex) - layout.getLineTop(lineIndex) if (durY + titlePaint.textSize < visibleHeight
+ contentPaint.fontMetrics.descent < visibleHeight
) { ) {
textPages.last().textLines.add(textLine) textPages.last().textLines.add(textLine)
durY = durY + layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex) durY += titlePaint.textSize + lineSpacingExtra
} 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)
//新页面 //新页面
durY = layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex) durY = titlePaint.textSize + lineSpacingExtra
textPages.add(TextPage()) textPages.add(TextPage())
textPages.last().textLines.add(textLine) textPages.last().textLines.add(textLine)
} }
textLine.lineTop = (paddingTop + durY - textLine.lineTop = paddingTop + durY - titlePaint.textSize
(layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex))).toFloat() textLine.lineBase = paddingTop + durY - titlePaint.fontMetrics.descent
textLine.lineBase = (paddingTop + durY - textLine.lineBottom = paddingTop + durY
(layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex))).toFloat()
textLine.lineBottom = textLine.lineBase + contentPaint.fontMetrics.descent
val words = val words =
title.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex)) title.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex))
stringBuilder.append(words) stringBuilder.append(words)
@ -165,40 +162,37 @@ object ChapterProvider {
*/ */
private fun joinBody( private fun joinBody(
text: String, text: String,
y: Int, y: Float,
textPages: ArrayList<TextPage>, textPages: ArrayList<TextPage>,
pageLines: ArrayList<Int>, pageLines: ArrayList<Int>,
pageLengths: ArrayList<Int>, pageLengths: ArrayList<Int>,
stringBuilder: StringBuilder stringBuilder: StringBuilder
): Int { ): Float {
var durY = y var durY = y
val layout = StaticLayout( val layout = StaticLayout(
text, contentPaint, visibleWidth, text, contentPaint, visibleWidth,
Layout.Alignment.ALIGN_NORMAL, 1f, lineSpacingExtra, 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 textPages.last().height = durY
val textLine = TextLine() val textLine = TextLine()
if (durY + layout.getLineBaseline(lineIndex) - layout.getLineTop(lineIndex) if (durY + contentPaint.textSize < visibleHeight
+ contentPaint.fontMetrics.descent < visibleHeight
) { ) {
textPages.last().textLines.add(textLine) textPages.last().textLines.add(textLine)
durY = durY + layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex) durY += contentPaint.textSize + lineSpacingExtra
} 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)
//新页面 //新页面
durY = layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex) durY = contentPaint.textSize + lineSpacingExtra
textPages.add(TextPage()) textPages.add(TextPage())
textPages.last().textLines.add(textLine) textPages.last().textLines.add(textLine)
} }
textLine.lineTop = (paddingTop + durY - textLine.lineTop = paddingTop + durY - titlePaint.textSize
(layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex))).toFloat() textLine.lineBase = paddingTop + durY - titlePaint.fontMetrics.descent
textLine.lineBase = (paddingTop + durY - textLine.lineBottom = paddingTop + durY
(layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex))).toFloat()
textLine.lineBottom = textLine.lineBase + contentPaint.fontMetrics.descent
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)
@ -346,7 +340,7 @@ object ChapterProvider {
val bold = if (ReadBookConfig.textBold) Typeface.BOLD else Typeface.NORMAL val bold = if (ReadBookConfig.textBold) Typeface.BOLD else Typeface.NORMAL
contentPaint.typeface = Typeface.create(typeface, bold) contentPaint.typeface = Typeface.create(typeface, bold)
//间距 //间距
lineSpacingExtra = ReadBookConfig.lineSpacingExtra.dp.toFloat() lineSpacingExtra = ReadBookConfig.lineSpacingExtra.dp
paragraphSpacing = ReadBookConfig.paragraphSpacing.dp paragraphSpacing = ReadBookConfig.paragraphSpacing.dp
titlePaint.textSize = (ReadBookConfig.textSize + 2).dp.toFloat() titlePaint.textSize = (ReadBookConfig.textSize + 2).dp.toFloat()
contentPaint.textSize = ReadBookConfig.textSize.dp.toFloat() contentPaint.textSize = ReadBookConfig.textSize.dp.toFloat()

@ -14,7 +14,7 @@ data class TextPage(
var pageSize: Int = 0, var pageSize: Int = 0,
var chapterSize: Int = 0, var chapterSize: Int = 0,
var chapterIndex: Int = 0, var chapterIndex: Int = 0,
var height: Int = 0 var height: Float = 0f
) { ) {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
@ -46,7 +46,7 @@ data class TextPage(
} }
textLines.add(textLine) textLines.add(textLine)
} }
height = ChapterProvider.visibleHeight height = ChapterProvider.visibleHeight.toFloat()
} }
return this return this
} }

Loading…
Cancel
Save