feat: 优化代码

pull/106/head
kunfei 5 years ago
parent da5fa596e3
commit f98d7a3577
  1. 3
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 32
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  3. 7
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt
  4. 31
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt

@ -5,7 +5,6 @@ import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.text.SpannableStringBuilder
import android.view.KeyEvent
import android.view.Menu
import android.view.MenuItem
@ -506,7 +505,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
if (it == Status.STOP || it == Status.PAUSE) {
ReadBook.curTextChapter?.let { textChapter ->
val page = textChapter.page(ReadBook.durPageIndex)
if (page != null && page.text is SpannableStringBuilder) {
if (page != null) {
page.removePageAloudSpan()
page_view.upContent()
}

@ -5,6 +5,7 @@ import android.graphics.Canvas
import android.util.AttributeSet
import android.view.View
import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.theme.accentColor
import io.legado.app.ui.book.read.page.entities.TextPage
@ -36,22 +37,23 @@ class ContentTextView : View {
super.onDraw(canvas)
textPage?.let { textPage ->
textPage.textLines.forEach { textLine ->
val textPaint = if (textLine.isTitle) {
ChapterProvider.titlePaint
} else {
ChapterProvider.contentPaint
}
textPaint.color = if (textLine.isReadAloud) {
context.accentColor
} else {
ReadBookConfig.durConfig.textColor()
}
textLine.textChars.forEach {
if (textLine.isTitle) {
canvas.drawText(
it.charData,
it.leftBottomPosition.x.toFloat(),
it.leftBottomPosition.y.toFloat(),
ChapterProvider.titlePaint
)
} else {
canvas.drawText(
it.charData,
it.leftBottomPosition.x.toFloat(),
it.leftBottomPosition.y.toFloat(),
ChapterProvider.contentPaint
)
}
canvas.drawText(
it.charData,
it.leftBottomPosition.x.toFloat(),
it.leftBottomPosition.y.toFloat(),
textPaint
)
}
}
}

@ -1,6 +1,5 @@
package io.legado.app.ui.book.read.page.entities
import android.text.SpannableStringBuilder
import kotlin.math.min
data class TextChapter(
@ -28,12 +27,12 @@ data class TextChapter(
fun scrollPage(): TextPage? {
if (pages.isNotEmpty()) {
val spannableStringBuilder = SpannableStringBuilder()
val stringBuilder = StringBuilder()
pages.forEach {
spannableStringBuilder.append(it.text)
stringBuilder.append(it.text)
}
return TextPage(
index = 0, text = spannableStringBuilder, title = title,
index = 0, text = stringBuilder.toString(), title = title,
pageSize = pages.size, chapterSize = chaptersSize, chapterIndex = position
)
}

@ -1,12 +1,11 @@
package io.legado.app.ui.book.read.page.entities
import android.text.SpannableStringBuilder
import io.legado.app.App
import io.legado.app.R
data class TextPage(
var index: Int = 0,
var text: CharSequence = App.INSTANCE.getString(R.string.data_loading),
var text: String = App.INSTANCE.getString(R.string.data_loading),
var title: String = "",
val textLines: ArrayList<TextLine> = arrayListOf(),
var pageSize: Int = 0,
@ -22,16 +21,28 @@ data class TextPage(
}
fun upPageAloudSpan(pageStart: Int) {
if (text is SpannableStringBuilder) {
removePageAloudSpan()
var lineStart = 0
for (textLine in textLines) {
if (pageStart > lineStart && pageStart < lineStart + textLine.text.length) {
textLine.isReadAloud = true
break
removePageAloudSpan()
var lineStart = 0
for ((index, textLine) in textLines.withIndex()) {
if (pageStart > lineStart && pageStart < lineStart + textLine.text.length) {
for (i in index - 1 downTo 0) {
if (textLines[i].text.endsWith("\n")) {
break
} else {
textLines[i].isReadAloud = true
}
}
lineStart += textLine.text.length
for (i in index until textLines.size) {
if (textLines[i].text.endsWith("\n")) {
textLines[i].isReadAloud = true
break
} else {
textLines[i].isReadAloud = true
}
}
break
}
lineStart += textLine.text.length
}
}
}

Loading…
Cancel
Save