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

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

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

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

Loading…
Cancel
Save