From f98d7a35778eee221b537a848376f29993fd3458 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 21 Feb 2020 08:34:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/read/ReadBookActivity.kt | 3 +- .../app/ui/book/read/page/ContentTextView.kt | 32 ++++++++++--------- .../ui/book/read/page/entities/TextChapter.kt | 7 ++-- .../ui/book/read/page/entities/TextPage.kt | 31 ++++++++++++------ 4 files changed, 42 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 790ad109b..9b05b7308 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.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(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() } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt index daf4e57da..7b8c2e45d 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt @@ -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 + ) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt index bdfd77490..e2cbaafde 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt @@ -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 ) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt index ef7c9c9f0..653f352e5 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt @@ -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 = 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 } } }