From 7c9a59db0188bcc9d4a5779e37a48c3efc366285 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 21 Feb 2020 09:56:24 +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/page/ChapterProvider.kt | 86 +++++++------------ .../app/ui/book/read/page/ContentTextView.kt | 2 +- .../ui/book/read/page/entities/TextChar.kt | 6 +- .../ui/book/read/page/entities/TextPoint.kt | 6 ++ 4 files changed, 41 insertions(+), 59 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPoint.kt diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt index 927c3eb5a..6124c5956 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt @@ -1,6 +1,5 @@ package io.legado.app.ui.book.read.page -import android.graphics.Point import android.graphics.Typeface import android.text.Layout import android.text.StaticLayout @@ -11,10 +10,7 @@ import io.legado.app.constant.PreferKey import io.legado.app.data.entities.BookChapter import io.legado.app.help.BookHelp import io.legado.app.help.ReadBookConfig -import io.legado.app.ui.book.read.page.entities.TextChapter -import io.legado.app.ui.book.read.page.entities.TextChar -import io.legado.app.ui.book.read.page.entities.TextLine -import io.legado.app.ui.book.read.page.entities.TextPage +import io.legado.app.ui.book.read.page.entities.* import io.legado.app.utils.dp import io.legado.app.utils.getPrefString import io.legado.app.utils.removePref @@ -169,12 +165,10 @@ object ChapterProvider { textPages.add(TextPage()) textPages.last().textLines.add(textLine) } - textLine.lineBottom = - paddingTop + durY - layout.getLineBottom(lineIndex) + layout.getLineBaseline( - lineIndex - ) - textLine.lineTop = - paddingTop + durY - layout.getLineBottom(lineIndex) + layout.getLineTop(lineIndex) + textLine.lineBottom = paddingTop + durY - + (layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex)) + textLine.lineTop = paddingTop + durY - + (layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex)) val words = title.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex)) stringBuilder.append(words) @@ -183,19 +177,15 @@ object ChapterProvider { if (lineIndex != layout.lineCount - 1) { val gapCount: Int = words.length - 1 val d = (visibleWidth - desiredWidth) / gapCount - var x = 0 + var x = 0f for (i in words.indices) { val char = words[i].toString() val cw = StaticLayout.getDesiredWidth(char, titlePaint) - val x1 = if (i != words.lastIndex) { - (x + cw + d).toInt() - } else { - (x + cw).toInt() - } + val x1 = if (i != words.lastIndex) (x + cw + d) else (x + cw) val textChar = TextChar( charData = char, - leftBottomPosition = Point(paddingLeft + x, textLine.lineBottom), - rightTopPosition = Point(paddingLeft + x1, textLine.lineTop) + leftBottomPosition = TextPoint(paddingLeft + x, textLine.lineBottom), + rightTopPosition = TextPoint(paddingLeft + x1, textLine.lineTop) ) textLine.textChars.add(textChar) x = x1 @@ -204,15 +194,15 @@ object ChapterProvider { //最后一行 textLine.text = "$words\n" stringBuilder.append("\n") - var x = 0 + var x = 0f for (i in words.indices) { val char = words[i].toString() val cw = StaticLayout.getDesiredWidth(char, titlePaint) - val x1 = (x + cw).toInt() + val x1 = x + cw val textChar = TextChar( charData = char, - leftBottomPosition = Point(paddingLeft + x, textLine.lineBottom), - rightTopPosition = Point(paddingLeft + x1, textLine.lineTop) + leftBottomPosition = TextPoint(paddingLeft + x, textLine.lineBottom), + rightTopPosition = TextPoint(paddingLeft + x1, textLine.lineTop) ) textLine.textChars.add(textChar) x = x1 @@ -253,14 +243,10 @@ object ChapterProvider { textPages.add(TextPage()) textPages.last().textLines.add(textLine) } - textLine.lineBottom = - paddingTop + durY - layout.getLineBottom(lineIndex) + layout.getLineBaseline( - lineIndex - ) - textLine.lineTop = - paddingTop + durY - layout.getLineBottom(lineIndex) + layout.getLineTop( - lineIndex - ) + textLine.lineBottom = paddingTop + durY - + (layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex)) + textLine.lineTop = paddingTop + durY - + (layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex)) var words = text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex)) stringBuilder.append(words) @@ -268,13 +254,13 @@ object ChapterProvider { val desiredWidth = layout.getLineMax(lineIndex) if (lineIndex == 0 && layout.lineCount > 1) { //第一行 - var x = 0 + var x = 0f val icw = StaticLayout.getDesiredWidth(bodyIndent, contentPaint) - var x1 = (x + icw).toInt() + var x1 = x + icw val textChar = TextChar( charData = bodyIndent, - leftBottomPosition = Point(paddingLeft + x, textLine.lineBottom), - rightTopPosition = Point(paddingLeft + x1, textLine.lineTop) + leftBottomPosition = TextPoint(paddingLeft + x, textLine.lineBottom), + rightTopPosition = TextPoint(paddingLeft + x1, textLine.lineTop) ) textLine.textChars.add(textChar) x = x1 @@ -284,15 +270,11 @@ object ChapterProvider { for (i in words.indices) { val char = words[i].toString() val cw = StaticLayout.getDesiredWidth(char, contentPaint) - x1 = if (i != words.lastIndex) { - (x + cw + d).toInt() - } else { - (x + cw).toInt() - } + x1 = if (i != words.lastIndex) x + cw + d else x + cw val textChar1 = TextChar( charData = char, - leftBottomPosition = Point(paddingLeft + x, textLine.lineBottom), - rightTopPosition = Point(paddingLeft + x1, textLine.lineTop) + leftBottomPosition = TextPoint(paddingLeft + x, textLine.lineBottom), + rightTopPosition = TextPoint(paddingLeft + x1, textLine.lineTop) ) textLine.textChars.add(textChar1) x = x1 @@ -301,15 +283,15 @@ object ChapterProvider { //最后一行 stringBuilder.append("\n") textLine.text = "$words\n" - var x = 0 + var x = 0f for (i in words.indices) { val char = words[i].toString() val cw = StaticLayout.getDesiredWidth(char, contentPaint) - val x1 = (x + cw).toInt() + val x1 = x + cw val textChar = TextChar( charData = char, - leftBottomPosition = Point(paddingLeft + x, textLine.lineBottom), - rightTopPosition = Point(paddingLeft + x1, textLine.lineTop) + leftBottomPosition = TextPoint(paddingLeft + x, textLine.lineBottom), + rightTopPosition = TextPoint(paddingLeft + x1, textLine.lineTop) ) textLine.textChars.add(textChar) x = x1 @@ -318,19 +300,15 @@ object ChapterProvider { //中间行 val gapCount: Int = words.length - 1 val d = (visibleWidth - desiredWidth) / gapCount - var x = 0 + var x = 0f for (i in words.indices) { val char = words[i].toString() val cw = StaticLayout.getDesiredWidth(char, contentPaint) - val x1 = if (i != words.lastIndex) { - (x + cw + d).toInt() - } else { - (x + cw).toInt() - } + val x1 = if (i != words.lastIndex) x + cw + d else x + cw val textChar = TextChar( charData = char, - leftBottomPosition = Point(paddingLeft + x, textLine.lineBottom), - rightTopPosition = Point(paddingLeft + x1, textLine.lineTop) + leftBottomPosition = TextPoint(paddingLeft + x, textLine.lineBottom), + rightTopPosition = TextPoint(paddingLeft + x1, textLine.lineTop) ) textLine.textChars.add(textChar) x = x1 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 7b8c2e45d..72d8fece6 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 @@ -50,7 +50,7 @@ class ContentTextView : View { textLine.textChars.forEach { canvas.drawText( it.charData, - it.leftBottomPosition.x.toFloat(), + it.leftBottomPosition.x, it.leftBottomPosition.y.toFloat(), textPaint ) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChar.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChar.kt index 38da6fcd6..9883e6dab 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChar.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChar.kt @@ -1,10 +1,8 @@ package io.legado.app.ui.book.read.page.entities -import android.graphics.Point - data class TextChar( val charData: String, var selected: Boolean = false, - val leftBottomPosition: Point, - val rightTopPosition: Point + val leftBottomPosition: TextPoint, + val rightTopPosition: TextPoint ) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPoint.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPoint.kt new file mode 100644 index 000000000..44b81c2a9 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPoint.kt @@ -0,0 +1,6 @@ +package io.legado.app.ui.book.read.page.entities + +data class TextPoint( + val x: Float, + val y: Int +) \ No newline at end of file