从文字选择处开始朗读

pull/2179/head
kunfei 2 years ago
parent 89d123449b
commit bf889d9360
  1. 5
      app/src/main/java/io/legado/app/model/ReadAloud.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  3. 9
      app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt
  4. 33
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt

@ -39,13 +39,14 @@ object ReadAloud {
fun play( fun play(
context: Context, context: Context,
play: Boolean = true, play: Boolean = true,
pageIndex: Int = ReadBook.durPageIndex pageIndex: Int = ReadBook.durPageIndex,
startPos: Int = 0
) { ) {
val intent = Intent(context, aloudClass) val intent = Intent(context, aloudClass)
intent.action = IntentAction.play intent.action = IntentAction.play
intent.putExtra("play", play) intent.putExtra("play", play)
intent.putExtra("pageIndex", pageIndex) intent.putExtra("pageIndex", pageIndex)
intent.putExtra("startPos", startPos)
context.startService(intent) context.startService(intent)
} }

@ -446,7 +446,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
return book.createBookMark().apply { return book.createBookMark().apply {
chapterIndex = page.chapterIndex chapterIndex = page.chapterIndex
chapterPos = chapter.getReadLength(page.index) + chapterPos = chapter.getReadLength(page.index) +
page.getSelectStartLength(selectStart.lineIndex, selectStart.charIndex) page.getPosByLineColumn(selectStart.lineIndex, selectStart.charIndex)
chapterName = chapter.title chapterName = chapter.title
bookText = getSelectedText() bookText = getSelectedText()
} }

@ -562,17 +562,16 @@ class ReadView(context: Context, attrs: AttributeSet) :
* 从选择位置开始朗读 * 从选择位置开始朗读
*/ */
fun aloudStartSelect() { fun aloudStartSelect() {
//todo 未完成
val selectStartPos = curPage.selectStartPos val selectStartPos = curPage.selectStartPos
curPage.textPage.textLines.forEach { val line = selectStartPos.lineIndex
val column = selectStartPos.charIndex
}
if (selectStartPos.relativePagePos > 0) { if (selectStartPos.relativePagePos > 0) {
if (!ReadBook.moveToNextPage()) { if (!ReadBook.moveToNextPage()) {
ReadBook.moveToNextChapter(false) ReadBook.moveToNextChapter(false)
} }
} }
ReadAloud.play(context) val startPos = curPage.textPage.getPosByLineColumn(line, column)
ReadAloud.play(context, startPos = startPos)
} }
/** /**

@ -34,6 +34,9 @@ data class TextPage(
} }
} }
/**
* 底部对齐更新行位置
*/
fun upLinesPosition() { fun upLinesPosition() {
if (!ReadBookConfig.textBottomJustify) return if (!ReadBookConfig.textBottomJustify) return
if (textLines.size <= 1) return if (textLines.size <= 1) return
@ -77,6 +80,9 @@ data class TextPage(
} }
} }
/**
* 计算文字位置
*/
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
fun format(): TextPage { fun format(): TextPage {
if (textLines.isEmpty()) isMsgPage = true if (textLines.isEmpty()) isMsgPage = true
@ -105,9 +111,7 @@ data class TextPage(
val cw = StaticLayout.getDesiredWidth(char, ChapterProvider.contentPaint) val cw = StaticLayout.getDesiredWidth(char, ChapterProvider.contentPaint)
val x1 = x + cw val x1 = x + cw
textLine.textChars.add( textLine.textChars.add(
TextChar( TextChar(char, start = x, end = x1)
char, start = x, end = x1
)
) )
x = x1 x = x1
} }
@ -118,6 +122,9 @@ data class TextPage(
return this return this
} }
/**
* 移除朗读标志
*/
fun removePageAloudSpan(): TextPage { fun removePageAloudSpan(): TextPage {
textLines.forEach { textLine -> textLines.forEach { textLine ->
textLine.isReadAloud = false textLine.isReadAloud = false
@ -125,6 +132,10 @@ data class TextPage(
return this return this
} }
/**
* 更新朗读标志
* @param aloudSpanStart 朗读文字开始位置
*/
fun upPageAloudSpan(aloudSpanStart: Int) { fun upPageAloudSpan(aloudSpanStart: Int) {
removePageAloudSpan() removePageAloudSpan()
var lineStart = 0 var lineStart = 0
@ -151,6 +162,9 @@ data class TextPage(
} }
} }
/**
* 阅读进度
*/
val readProgress: String val readProgress: String
get() { get() {
val df = DecimalFormat("0.0%") val df = DecimalFormat("0.0%")
@ -167,15 +181,24 @@ data class TextPage(
return percent return percent
} }
fun getSelectStartLength(lineIndex: Int, charIndex: Int): Int { /**
* 根据行和列返回字符在本页的位置
* @param lineIndex 字符在第几行
* @param columnIndex 字符在第几列
* @return 字符在本页位置
*/
fun getPosByLineColumn(lineIndex: Int, columnIndex: Int): Int {
var length = 0 var length = 0
val maxIndex = min(lineIndex, lineSize) val maxIndex = min(lineIndex, lineSize)
for (index in 0 until maxIndex) { for (index in 0 until maxIndex) {
length += textLines[index].charSize length += textLines[index].charSize
} }
return length + charIndex return length + columnIndex
} }
/**
* @return 页面所在章节
*/
fun getTextChapter(): TextChapter? { fun getTextChapter(): TextChapter? {
ReadBook.curTextChapter?.let { ReadBook.curTextChapter?.let {
if (it.position == chapterIndex) { if (it.position == chapterIndex) {

Loading…
Cancel
Save