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 559997c02..74136ba5c 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 @@ -119,12 +119,12 @@ object ChapterProvider { Layout.Alignment.ALIGN_NORMAL, 1f, lineSpacingExtra, true ) for (lineIndex in 0 until layout.lineCount) { + textPages.last().height = durY durY = durY + layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex) val textLine = TextLine(isTitle = true) if (durY < visibleHeight) { textPages.last().textLines.add(textLine) } else { - textPages.last().height = durY textPages.last().text = stringBuilder.toString() stringBuilder.clear() pageLines.add(textPages.last().textLines.size) @@ -202,12 +202,12 @@ object ChapterProvider { Layout.Alignment.ALIGN_NORMAL, 1f, lineSpacingExtra, true ) for (lineIndex in 0 until layout.lineCount) { - val textLine = TextLine(isTitle = false) + textPages.last().height = durY durY = durY + layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex) + val textLine = TextLine() if (durY < visibleHeight) { textPages.last().textLines.add(textLine) } else { - textPages.last().height = durY textPages.last().text = stringBuilder.toString() stringBuilder.clear() pageLines.add(textPages.last().textLines.size) 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 5abb7175f..28b803b90 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 @@ -77,7 +77,12 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at ReadBookConfig.durConfig.textColor() } textLine.textChars.forEach { - canvas.drawText(it.charData, it.start, textLine.lineBase, textPaint) + canvas.drawText( + it.charData, + it.start, + textLine.lineBase, + textPaint + ) if (it.selected) { canvas.drawRect( it.start, @@ -92,11 +97,101 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at } private fun drawScrollPage(canvas: Canvas) { - + if (pageOffset <= 0) { + 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 { + canvas.drawText( + it.charData, + it.start, + textLine.lineBase + pageOffset, + textPaint + ) + if (it.selected) { + canvas.drawRect( + it.start, + textLine.lineTop + pageOffset, + it.end, + textLine.lineBottom + pageOffset, + selectedPaint + ) + } + } + } + pageFactory.nextPage?.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 { + canvas.drawText( + it.charData, + it.start, + textLine.lineBase + pageOffset + textPage.height - ChapterProvider.paddingTop, + textPaint + ) + if (it.selected) { + canvas.drawRect( + it.start, + textLine.lineTop + pageOffset + textPage.height - ChapterProvider.paddingTop, + it.end, + textLine.lineBottom + pageOffset + textPage.height - ChapterProvider.paddingTop, + selectedPaint + ) + } + } + } + } else { + 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 { + canvas.drawText( + it.charData, + it.start, + textLine.lineBase + pageOffset, + textPaint + ) + if (it.selected) { + canvas.drawRect( + it.start, + textLine.lineTop + pageOffset, + it.end, + textLine.lineBottom + pageOffset, + selectedPaint + ) + } + } + } + } } fun onScroll(mOffset: Float) { - var offset = mOffset + if (mOffset == 0f) return + var offset = -mOffset if (offset > maxScrollOffset) { offset = maxScrollOffset } else if (offset < -maxScrollOffset) { @@ -104,6 +199,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at } pageOffset += offset + invalidate() } fun resetPageOffset() { diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/PageFactory.kt b/app/src/main/java/io/legado/app/ui/book/read/page/PageFactory.kt index 6f39f2992..a335505ff 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/PageFactory.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/PageFactory.kt @@ -10,11 +10,11 @@ abstract class PageFactory(protected val dataSource: DataSource) { abstract fun moveToPrevious(): Boolean - abstract fun nextPage(): DATA? + abstract val nextPage: DATA? - abstract fun previousPage(): DATA? + abstract val prevPage: DATA? - abstract fun currentPage(): DATA? + abstract val currentPage: DATA? abstract fun hasNext(): Boolean diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt index b2fc6fad4..ac7671406 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt @@ -100,15 +100,15 @@ class PageView(context: Context, attrs: AttributeSet) : fun upContent(position: Int = 0) { if (ReadBookConfig.isScroll) { - curPage.setContent(pageFactory.currentPage()) + curPage.setContent(pageFactory.currentPage) } else { when (position) { - -1 -> prevPage.setContent(pageFactory.previousPage()) - 1 -> nextPage.setContent(pageFactory.nextPage()) + -1 -> prevPage.setContent(pageFactory.prevPage) + 1 -> nextPage.setContent(pageFactory.nextPage) else -> { - curPage.setContent(pageFactory.currentPage()) - nextPage.setContent(pageFactory.nextPage()) - prevPage.setContent(pageFactory.previousPage()) + curPage.setContent(pageFactory.currentPage) + nextPage.setContent(pageFactory.nextPage) + prevPage.setContent(pageFactory.prevPage) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt b/app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt index 018f75bab..cfcf3f6c9 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt @@ -52,11 +52,13 @@ class TextPageFactory(dataSource: DataSource) : PageFactory(dataSource false } - override fun currentPage(): TextPage? = with(dataSource) { + override val currentPage: TextPage? + get() = with(dataSource) { return getCurrentChapter()?.page(pageIndex) } - override fun nextPage(): TextPage? = with(dataSource) { + override val nextPage: TextPage? + get() = with(dataSource) { getCurrentChapter()?.let { if (pageIndex < it.pageSize() - 1) { return getCurrentChapter()?.page(pageIndex + 1)?.removePageAloudSpan() @@ -65,7 +67,8 @@ class TextPageFactory(dataSource: DataSource) : PageFactory(dataSource return getNextChapter()?.page(0)?.removePageAloudSpan() } - override fun previousPage(): TextPage? = with(dataSource) { + override val prevPage: TextPage? + get() = with(dataSource) { if (pageIndex > 0) { return getCurrentChapter()?.page(pageIndex - 1)?.removePageAloudSpan() }