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

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

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

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