Просмотр исходного кода

从文字选择处开始朗读

kunfei 4 лет назад
Родитель
Сommit
1d0bb9bf3f

+ 3 - 2
app/src/main/java/io/legado/app/model/ReadAloud.kt

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

+ 1 - 1
app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt

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

+ 4 - 5
app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt

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

+ 28 - 5
app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt

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