From d590cc186a024246ec5df1f48348d1eab2fc43f2 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 24 Feb 2020 09:56:53 +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 | 8 ++-- .../app/ui/book/read/page/ContentTextView.kt | 28 ++++---------- .../ui/book/read/page/entities/TextPage.kt | 37 +++++++++++++++++++ 3 files changed, 48 insertions(+), 25 deletions(-) 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 cd8c5c0e8..559997c02 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 @@ -23,10 +23,10 @@ import io.legado.app.utils.removePref object ChapterProvider { var viewWidth = 0 var viewHeight = 0 - private var visibleWidth = 0 - private var visibleHeight = 0 - private var paddingLeft = 0 - private var paddingTop = 0 + var visibleWidth = 0 + var visibleHeight = 0 + var paddingLeft = 0 + var paddingTop = 0 private var lineSpacingExtra = 0f private var paragraphSpacing = 0 var typeface: Typeface = Typeface.SANS_SERIF 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 8d8d9632d..404413fe0 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 @@ -3,8 +3,6 @@ package io.legado.app.ui.book.read.page import android.content.Context import android.graphics.Canvas import android.graphics.Paint -import android.text.Layout -import android.text.StaticLayout import android.util.AttributeSet import android.view.View import io.legado.app.R @@ -30,7 +28,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at private var selectCharStart = 0 private var selectLineEnd = 0 private var selectCharEnd = 0 - private var textPage: TextPage = TextPage() + private var textPage: TextPage = TextPage().textToLine() //滚动参数 private val pageFactory: TextPageFactory get() = callBack.pageFactory private val maxScrollOffset = 100f @@ -60,29 +58,13 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at override fun onDraw(canvas: Canvas) { super.onDraw(canvas) - - if (textPage.textLines.isEmpty()) { - drawMsg(canvas, textPage.text) + if (ReadBookConfig.isScroll) { + drawScrollPage(canvas) } else { drawHorizontalPage(canvas) } } - @Suppress("DEPRECATION") - private fun drawMsg(canvas: Canvas, msg: String) { - val layout = StaticLayout( - msg, ChapterProvider.contentPaint, width, - Layout.Alignment.ALIGN_NORMAL, 1f, 0f, false - ) - val y = (height - layout.height) / 2f - for (lineIndex in 0 until layout.lineCount) { - val x = (width - layout.getLineMax(lineIndex)) / 2 - val words = - msg.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex)) - canvas.drawText(words, x, y, ChapterProvider.contentPaint) - } - } - private fun drawHorizontalPage(canvas: Canvas) { textPage.textLines.forEach { textLine -> val textPaint = if (textLine.isTitle) { @@ -110,6 +92,10 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at } } + private fun drawScrollPage(canvas: Canvas) { + + } + fun onScroll(mOffset: Float) { var offset = mOffset if (offset > maxScrollOffset) { 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 92d91b1e5..c52a0834e 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,7 +1,10 @@ package io.legado.app.ui.book.read.page.entities +import android.text.Layout +import android.text.StaticLayout import io.legado.app.App import io.legado.app.R +import io.legado.app.ui.book.read.page.ChapterProvider data class TextPage( var index: Int = 0, @@ -14,6 +17,40 @@ data class TextPage( var height: Int = 0 ) { + @Suppress("DEPRECATION") + fun textToLine(): TextPage { + if (textLines.isEmpty()) { + val layout = StaticLayout( + text, ChapterProvider.contentPaint, ChapterProvider.visibleWidth, + Layout.Alignment.ALIGN_NORMAL, 1f, 0f, false + ) + var y = (ChapterProvider.visibleHeight - layout.height) / 2f + if (y < 0) y = 0f + for (lineIndex in 0 until layout.lineCount) { + val textLine = TextLine() + textLine.lineTop = (ChapterProvider.paddingTop + y - + (layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex))) + textLine.lineBase = (ChapterProvider.paddingTop + y - + (layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex))) + textLine.lineBottom = + textLine.lineBase + ChapterProvider.contentPaint.fontMetrics.descent + var x = (ChapterProvider.visibleWidth - layout.getLineMax(lineIndex)) / 2 + textLine.text = + text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex)) + for (i in textLine.text.indices) { + val char = textLine.text[i].toString() + val cw = StaticLayout.getDesiredWidth(char, ChapterProvider.contentPaint) + val x1 = x + cw + textLine.textChars.add(TextChar(charData = char, start = x, end = x1)) + x = x1 + } + textLines.add(textLine) + } + height = ChapterProvider.visibleHeight + } + return this + } + fun removePageAloudSpan(): TextPage { textLines.forEach { textLine -> textLine.isReadAloud = false