feat: 优化代码

pull/115/head
kunfei 5 years ago
parent a095b75d2d
commit 702b0d0022
  1. 41
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt
  2. 36
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  3. 4
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChar.kt
  4. 1
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt
  5. 6
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPoint.kt

@ -10,7 +10,10 @@ import io.legado.app.constant.PreferKey
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.ui.book.read.page.entities.* import io.legado.app.ui.book.read.page.entities.TextChapter
import io.legado.app.ui.book.read.page.entities.TextChar
import io.legado.app.ui.book.read.page.entities.TextLine
import io.legado.app.ui.book.read.page.entities.TextPage
import io.legado.app.utils.dp import io.legado.app.utils.dp
import io.legado.app.utils.getPrefString import io.legado.app.utils.getPrefString
import io.legado.app.utils.removePref import io.legado.app.utils.removePref
@ -131,10 +134,11 @@ object ChapterProvider {
textPages.add(TextPage()) textPages.add(TextPage())
textPages.last().textLines.add(textLine) textPages.last().textLines.add(textLine)
} }
textLine.lineBottom = (paddingTop + durY -
(layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex))).toFloat()
textLine.lineTop = (paddingTop + durY - textLine.lineTop = (paddingTop + durY -
(layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex))).toFloat() (layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex))).toFloat()
textLine.lineBase = (paddingTop + durY -
(layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex))).toFloat()
textLine.lineBottom = textLine.lineBase + titlePaint.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)
@ -150,8 +154,8 @@ object ChapterProvider {
val x1 = if (i != words.lastIndex) (x + cw + d) else (x + cw) val x1 = if (i != words.lastIndex) (x + cw + d) else (x + cw)
val textChar = TextChar( val textChar = TextChar(
charData = char, charData = char,
leftBottomPosition = TextPoint(paddingLeft + x, textLine.lineBottom), start = paddingLeft + x,
rightTopPosition = TextPoint(paddingLeft + x1, textLine.lineTop) end = paddingLeft + x1
) )
textLine.textChars.add(textChar) textLine.textChars.add(textChar)
x = x1 x = x1
@ -169,14 +173,13 @@ object ChapterProvider {
val x1 = x + cw val x1 = x + cw
val textChar = TextChar( val textChar = TextChar(
charData = char, charData = char,
leftBottomPosition = TextPoint(paddingLeft + x, textLine.lineBottom), start = paddingLeft + x,
rightTopPosition = TextPoint(paddingLeft + x1, textLine.lineTop) end = paddingLeft + x1
) )
textLine.textChars.add(textChar) textLine.textChars.add(textChar)
x = x1 x = x1
} }
} }
textLine.lineBottom = textLine.lineBottom + titlePaint.fontMetrics.descent
} }
durY += paragraphSpacing durY += paragraphSpacing
return durY return durY
@ -214,10 +217,11 @@ object ChapterProvider {
textPages.add(TextPage()) textPages.add(TextPage())
textPages.last().textLines.add(textLine) textPages.last().textLines.add(textLine)
} }
textLine.lineBottom = (paddingTop + durY -
(layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex))).toFloat()
textLine.lineTop = (paddingTop + durY - textLine.lineTop = (paddingTop + durY -
(layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex))).toFloat() (layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex))).toFloat()
textLine.lineBase = (paddingTop + durY -
(layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex))).toFloat()
textLine.lineBottom = textLine.lineBase + contentPaint.fontMetrics.descent
var words = var words =
text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex)) text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex))
stringBuilder.append(words) stringBuilder.append(words)
@ -231,8 +235,8 @@ object ChapterProvider {
val x1 = x + icw val x1 = x + icw
val textChar = TextChar( val textChar = TextChar(
charData = bodyIndent[i].toString(), charData = bodyIndent[i].toString(),
leftBottomPosition = TextPoint(paddingLeft + x, textLine.lineBottom), start = paddingLeft + x,
rightTopPosition = TextPoint(paddingLeft + x1, textLine.lineTop) end = paddingLeft + x1
) )
textLine.textChars.add(textChar) textLine.textChars.add(textChar)
x = x1 x = x1
@ -246,8 +250,8 @@ object ChapterProvider {
val x1 = if (i != words.lastIndex) x + cw + d else x + cw val x1 = if (i != words.lastIndex) x + cw + d else x + cw
val textChar1 = TextChar( val textChar1 = TextChar(
charData = char, charData = char,
leftBottomPosition = TextPoint(paddingLeft + x, textLine.lineBottom), start = paddingLeft + x,
rightTopPosition = TextPoint(paddingLeft + x1, textLine.lineTop) end = paddingLeft + x1
) )
textLine.textChars.add(textChar1) textLine.textChars.add(textChar1)
x = x1 x = x1
@ -263,8 +267,8 @@ object ChapterProvider {
val x1 = x + cw val x1 = x + cw
val textChar = TextChar( val textChar = TextChar(
charData = char, charData = char,
leftBottomPosition = TextPoint(paddingLeft + x, textLine.lineBottom), start = paddingLeft + x,
rightTopPosition = TextPoint(paddingLeft + x1, textLine.lineTop) end = paddingLeft + x1
) )
textLine.textChars.add(textChar) textLine.textChars.add(textChar)
x = x1 x = x1
@ -280,14 +284,13 @@ object ChapterProvider {
val x1 = if (i != words.lastIndex) x + cw + d else x + cw val x1 = if (i != words.lastIndex) x + cw + d else x + cw
val textChar = TextChar( val textChar = TextChar(
charData = char, charData = char,
leftBottomPosition = TextPoint(paddingLeft + x, textLine.lineBottom), start = paddingLeft + x,
rightTopPosition = TextPoint(paddingLeft + x1, textLine.lineTop) end = paddingLeft + x1
) )
textLine.textChars.add(textChar) textLine.textChars.add(textChar)
x = x1 x = x1
} }
} }
textLine.lineBottom = textLine.lineBottom + contentPaint.fontMetrics.descent
} }
durY += paragraphSpacing durY += paragraphSpacing
return durY return durY

@ -95,17 +95,12 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
ReadBookConfig.durConfig.textColor() ReadBookConfig.durConfig.textColor()
} }
textLine.textChars.forEach { textLine.textChars.forEach {
canvas.drawText( canvas.drawText(it.charData, it.start, textLine.lineBase, textPaint)
it.charData,
it.leftBottomPosition.x,
it.leftBottomPosition.y,
textPaint
)
if (it.selected) { if (it.selected) {
canvas.drawRect( canvas.drawRect(
it.leftBottomPosition.x, it.start,
textLine.lineTop, textLine.lineTop,
it.rightTopPosition.x, it.end,
textLine.lineBottom, textLine.lineBottom,
selectedPaint selectedPaint
) )
@ -158,21 +153,15 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
for ((lineIndex, textLine) in textPage.textLines.withIndex()) { for ((lineIndex, textLine) in textPage.textLines.withIndex()) {
if (y > textLine.lineTop && y < textLine.lineBottom) { if (y > textLine.lineTop && y < textLine.lineBottom) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) { if (x > textChar.start && x < textChar.end) {
textChar.selected = true textChar.selected = true
invalidate() invalidate()
selectLineStart = lineIndex selectLineStart = lineIndex
selectCharStart = charIndex selectCharStart = charIndex
selectLineEnd = lineIndex selectLineEnd = lineIndex
selectCharEnd = charIndex selectCharEnd = charIndex
upSelectedStart( upSelectedStart(textChar.start, textLine.lineBottom)
textChar.leftBottomPosition.x, upSelectedEnd(textChar.end, textLine.lineBottom)
textChar.leftBottomPosition.y
)
upSelectedEnd(
textChar.rightTopPosition.x,
textChar.leftBottomPosition.y
)
return true return true
} }
} }
@ -186,14 +175,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
for ((lineIndex, textLine) in textPage.textLines.withIndex()) { for ((lineIndex, textLine) in textPage.textLines.withIndex()) {
if (y > textLine.lineTop && y < textLine.lineBottom) { if (y > textLine.lineTop && y < textLine.lineBottom) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) { if (x > textChar.start && x < textChar.end) {
if (selectLineStart != lineIndex || selectCharStart != charIndex) { if (selectLineStart != lineIndex || selectCharStart != charIndex) {
selectLineStart = lineIndex selectLineStart = lineIndex
selectCharStart = charIndex selectCharStart = charIndex
upSelectedStart( upSelectedStart(textChar.start, textLine.lineBottom)
textChar.leftBottomPosition.x,
textChar.leftBottomPosition.y
)
upSelectChars(textPage) upSelectChars(textPage)
} }
break break
@ -208,13 +194,13 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
for ((lineIndex, textLine) in textPage.textLines.withIndex()) { for ((lineIndex, textLine) in textPage.textLines.withIndex()) {
if (y > textLine.lineTop && y < textLine.lineBottom) { if (y > textLine.lineTop && y < textLine.lineBottom) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) { if (x > textChar.start && x < textChar.end) {
if (selectLineEnd != lineIndex || selectCharEnd != charIndex) { if (selectLineEnd != lineIndex || selectCharEnd != charIndex) {
selectLineEnd = lineIndex selectLineEnd = lineIndex
selectCharEnd = charIndex selectCharEnd = charIndex
upSelectedEnd( upSelectedEnd(
textChar.rightTopPosition.x, textChar.end,
textChar.leftBottomPosition.y textLine.lineBottom
) )
upSelectChars(textPage) upSelectChars(textPage)
} }

@ -3,6 +3,6 @@ package io.legado.app.ui.book.read.page.entities
data class TextChar( data class TextChar(
val charData: String, val charData: String,
var selected: Boolean = false, var selected: Boolean = false,
val leftBottomPosition: TextPoint, val start: Float,
val rightTopPosition: TextPoint val end: Float
) )

@ -4,6 +4,7 @@ data class TextLine(
var text: String = "", var text: String = "",
val textChars: ArrayList<TextChar> = arrayListOf(), val textChars: ArrayList<TextChar> = arrayListOf(),
var lineTop: Float = 0f, var lineTop: Float = 0f,
var lineBase: Float = 0f,
var lineBottom: Float = 0f, var lineBottom: Float = 0f,
val isTitle: Boolean = false, val isTitle: Boolean = false,
var isReadAloud: Boolean = false var isReadAloud: Boolean = false

@ -1,6 +0,0 @@
package io.legado.app.ui.book.read.page.entities
data class TextPoint(
val x: Float,
val y: Float
)
Loading…
Cancel
Save