From 573812adf7326b8be0029d43899c93c5eb71ccb0 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 22 Feb 2020 21:37:55 +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 | 93 +++++++++++-------- .../app/ui/book/read/page/ContentTextView.kt | 27 ++++++ 2 files changed, 79 insertions(+), 41 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 293790152..3215c5464 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 @@ -35,47 +35,9 @@ object ChapterProvider { upStyle(ReadBookConfig.durConfig) } - fun upStyle(config: ReadBookConfig.Config) { - typeface = try { - val fontPath = App.INSTANCE.getPrefString(PreferKey.readBookFont) - if (!TextUtils.isEmpty(fontPath)) { - Typeface.createFromFile(fontPath) - } else { - Typeface.SANS_SERIF - } - } catch (e: Exception) { - App.INSTANCE.removePref(PreferKey.readBookFont) - Typeface.SANS_SERIF - } - //标题 - titlePaint.isAntiAlias = true - titlePaint.color = config.textColor() - titlePaint.letterSpacing = config.letterSpacing - titlePaint.typeface = Typeface.create(typeface, Typeface.BOLD) - //正文 - contentPaint.isAntiAlias = true - contentPaint.color = config.textColor() - contentPaint.letterSpacing = config.letterSpacing - val bold = if (config.textBold) Typeface.BOLD else Typeface.NORMAL - contentPaint.typeface = Typeface.create(typeface, bold) - //间距 - lineSpacingExtra = config.lineSpacingExtra.dp.toFloat() - paragraphSpacing = config.paragraphSpacing.dp - titlePaint.textSize = (config.textSize + 2).dp.toFloat() - contentPaint.textSize = config.textSize.dp.toFloat() - - bodyIndent = BookHelp.bodyIndent - - upSize(config) - } - - fun upSize(config: ReadBookConfig.Config) { - paddingLeft = config.paddingLeft.dp - paddingTop = config.paddingTop.dp - visibleWidth = viewWidth - paddingLeft - config.paddingRight.dp - visibleHeight = viewHeight - paddingTop - config.paddingBottom.dp - } - + /** + * 获取拆分完的章节数据 + */ fun getTextChapter( bookChapter: BookChapter, content: String, @@ -328,4 +290,53 @@ object ChapterProvider { durY += paragraphSpacing return durY } + + + /** + * 更新样式 + */ + fun upStyle(config: ReadBookConfig.Config) { + typeface = try { + val fontPath = App.INSTANCE.getPrefString(PreferKey.readBookFont) + if (!TextUtils.isEmpty(fontPath)) { + Typeface.createFromFile(fontPath) + } else { + Typeface.SANS_SERIF + } + } catch (e: Exception) { + App.INSTANCE.removePref(PreferKey.readBookFont) + Typeface.SANS_SERIF + } + //标题 + titlePaint.isAntiAlias = true + titlePaint.color = config.textColor() + titlePaint.letterSpacing = config.letterSpacing + titlePaint.typeface = Typeface.create(typeface, Typeface.BOLD) + //正文 + contentPaint.isAntiAlias = true + contentPaint.color = config.textColor() + contentPaint.letterSpacing = config.letterSpacing + val bold = if (config.textBold) Typeface.BOLD else Typeface.NORMAL + contentPaint.typeface = Typeface.create(typeface, bold) + //间距 + lineSpacingExtra = config.lineSpacingExtra.dp.toFloat() + paragraphSpacing = config.paragraphSpacing.dp + titlePaint.textSize = (config.textSize + 2).dp.toFloat() + contentPaint.textSize = config.textSize.dp.toFloat() + + bodyIndent = BookHelp.bodyIndent + + upSize(config) + } + + /** + * 更新View尺寸 + */ + fun upSize(config: ReadBookConfig.Config) { + paddingLeft = config.paddingLeft.dp + paddingTop = config.paddingTop.dp + visibleWidth = viewWidth - paddingLeft - config.paddingRight.dp + visibleHeight = viewHeight - paddingTop - config.paddingBottom.dp + } + } \ No newline at end of file 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 ffb16b571..c25fd0664 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,6 +3,8 @@ 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 @@ -37,10 +39,12 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at init { activityCallBack = activity as CallBack + contentDescription = textPage.text } fun setContent(textPage: TextPage) { this.textPage = textPage + contentDescription = textPage.text invalidate() } @@ -55,6 +59,29 @@ 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) + } 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) { ChapterProvider.titlePaint