feat: 优化代码

pull/112/head
kunfei 5 years ago
parent f06d77de89
commit 573812adf7
  1. 93
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt
  2. 27
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt

@ -35,47 +35,9 @@ object ChapterProvider {
upStyle(ReadBookConfig.durConfig) 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( fun getTextChapter(
bookChapter: BookChapter, bookChapter: BookChapter,
content: String, content: String,
@ -328,4 +290,53 @@ object ChapterProvider {
durY += paragraphSpacing durY += paragraphSpacing
return durY 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
}
} }

@ -3,6 +3,8 @@ package io.legado.app.ui.book.read.page
import android.content.Context import android.content.Context
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Paint import android.graphics.Paint
import android.text.Layout
import android.text.StaticLayout
import android.util.AttributeSet import android.util.AttributeSet
import android.view.View import android.view.View
import io.legado.app.R import io.legado.app.R
@ -37,10 +39,12 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
init { init {
activityCallBack = activity as CallBack activityCallBack = activity as CallBack
contentDescription = textPage.text
} }
fun setContent(textPage: TextPage) { fun setContent(textPage: TextPage) {
this.textPage = textPage this.textPage = textPage
contentDescription = textPage.text
invalidate() invalidate()
} }
@ -55,6 +59,29 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
super.onDraw(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 -> textPage.textLines.forEach { textLine ->
val textPaint = if (textLine.isTitle) { val textPaint = if (textLine.isTitle) {
ChapterProvider.titlePaint ChapterProvider.titlePaint

Loading…
Cancel
Save