feat: 优化代码

pull/111/head
kunfei 5 years ago
parent 0adf736195
commit 79293667db
  1. 4
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  2. 22
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt
  3. 6
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt

@ -17,6 +17,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.jetbrains.anko.toast
object ReadBook { object ReadBook {
@ -283,6 +284,9 @@ object ReadBook {
} }
} }
} }
}.onError {
it.printStackTrace()
App.INSTANCE.toast(it.localizedMessage ?: "ChapterProvider ERROR")
} }
} }

@ -257,22 +257,24 @@ object ChapterProvider {
if (lineIndex == 0 && layout.lineCount > 1) { if (lineIndex == 0 && layout.lineCount > 1) {
//第一行 //第一行
var x = 0f var x = 0f
val icw = StaticLayout.getDesiredWidth(bodyIndent, contentPaint) val icw = StaticLayout.getDesiredWidth(bodyIndent, contentPaint) / bodyIndent.length
var x1 = x + icw for (i in 0..bodyIndent.lastIndex) {
val textChar = TextChar( val x1 = x + icw
charData = bodyIndent, val textChar = TextChar(
leftBottomPosition = TextPoint(paddingLeft + x, textLine.lineBottom), charData = bodyIndent[i].toString(),
rightTopPosition = TextPoint(paddingLeft + x1, textLine.lineTop) leftBottomPosition = TextPoint(paddingLeft + x, textLine.lineBottom),
) rightTopPosition = TextPoint(paddingLeft + x1, textLine.lineTop)
textLine.textChars.add(textChar) )
x = x1 textLine.textChars.add(textChar)
x = x1
}
words = words.replaceFirst(bodyIndent, "") words = words.replaceFirst(bodyIndent, "")
val gapCount: Int = words.length - 1 val gapCount: Int = words.length - 1
val d = (visibleWidth - desiredWidth) / gapCount val d = (visibleWidth - desiredWidth) / gapCount
for (i in words.indices) { for (i in words.indices) {
val char = words[i].toString() val char = words[i].toString()
val cw = StaticLayout.getDesiredWidth(char, contentPaint) val cw = StaticLayout.getDesiredWidth(char, contentPaint)
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), leftBottomPosition = TextPoint(paddingLeft + x, textLine.lineBottom),

@ -207,12 +207,12 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (lineIndex == selectLineStart && lineIndex == selectLineEnd) { if (lineIndex == selectLineStart && lineIndex == selectLineEnd) {
stringBuilder.append( stringBuilder.append(
it.textLines[lineIndex].text.substring( it.textLines[lineIndex].text.substring(
selectCharStart + 1, selectCharStart,
selectCharEnd + 2 selectCharEnd + 1
) )
) )
} else if (lineIndex == selectLineStart) { } else if (lineIndex == selectLineStart) {
stringBuilder.append(it.textLines[lineIndex].text.substring(selectCharStart + 1)) stringBuilder.append(it.textLines[lineIndex].text.substring(selectCharStart))
} else if (lineIndex == selectLineEnd) { } else if (lineIndex == selectLineEnd) {
stringBuilder.append( stringBuilder.append(
it.textLines[lineIndex].text.substring(0, selectCharEnd + 1) it.textLines[lineIndex].text.substring(0, selectCharEnd + 1)

Loading…
Cancel
Save