feat: 优化代码

pull/115/head
kunfei 5 years ago
parent 2ee5376abc
commit 2864bc28ea
  1. 175
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt

@ -9,6 +9,7 @@ import io.legado.app.R
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.accentColor
import io.legado.app.ui.book.read.page.entities.TextChar
import io.legado.app.ui.book.read.page.entities.TextPage import io.legado.app.ui.book.read.page.entities.TextPage
import io.legado.app.utils.activity import io.legado.app.utils.activity
import io.legado.app.utils.getCompatColor import io.legado.app.utils.getCompatColor
@ -66,125 +67,82 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
private fun drawHorizontalPage(canvas: Canvas) { private fun drawHorizontalPage(canvas: Canvas) {
textPage.textLines.forEach { textLine -> textPage.textLines.forEach { textLine ->
val textPaint = if (textLine.isTitle) { drawChars(
ChapterProvider.titlePaint canvas,
} else { textLine.textChars,
ChapterProvider.contentPaint
}
textPaint.color = if (textLine.isReadAloud) {
context.accentColor
} else {
ReadBookConfig.durConfig.textColor()
}
textLine.textChars.forEach {
canvas.drawText(
it.charData,
it.start,
textLine.lineBase,
textPaint
)
if (it.selected) {
canvas.drawRect(
it.start,
textLine.lineTop, textLine.lineTop,
it.end, textLine.lineBase,
textLine.lineBottom, textLine.lineBottom,
selectedPaint textLine.isTitle,
textLine.isReadAloud
) )
} }
} }
}
}
private fun drawScrollPage(canvas: Canvas) { private fun drawScrollPage(canvas: Canvas) {
if (pageOffset <= 0) {
textPage.textLines.forEach { textLine -> textPage.textLines.forEach { textLine ->
val textPaint = if (textLine.isTitle) { val yPy = pageOffset
ChapterProvider.titlePaint val lineTop = textLine.lineTop + yPy
} else { val lineBase = textLine.lineBase + yPy
ChapterProvider.contentPaint val lineBottom = textLine.lineBottom + yPy
} drawChars(
textPaint.color = if (textLine.isReadAloud) { canvas,
context.accentColor textLine.textChars,
} else { lineTop,
ReadBookConfig.durConfig.textColor() lineBase,
} lineBottom,
textLine.textChars.forEach { textLine.isTitle,
canvas.drawText( textLine.isReadAloud
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 -> pageFactory.nextPage?.textLines?.forEach { textLine ->
val textPaint = if (textLine.isTitle) { val yPy = pageOffset + textPage.height - ChapterProvider.paddingTop
ChapterProvider.titlePaint val lineTop = textLine.lineTop + yPy
} else { val lineBase = textLine.lineBase + yPy
ChapterProvider.contentPaint val lineBottom = textLine.lineBottom + yPy
} drawChars(
textPaint.color = if (textLine.isReadAloud) { canvas,
context.accentColor textLine.textChars,
} else { lineTop,
ReadBookConfig.durConfig.textColor() lineBase,
} lineBottom,
textLine.textChars.forEach { textLine.isTitle,
canvas.drawText( textLine.isReadAloud
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
) )
} }
} pageFactory.prevPage?.textLines?.forEach { textLine ->
} val yPy = pageOffset + ChapterProvider.paddingTop
} else { val lineTop = -textLine.lineTop + yPy
textPage.textLines.forEach { textLine -> val lineBase = -textLine.lineBase + yPy
val textPaint = if (textLine.isTitle) { val lineBottom = -textLine.lineBottom + yPy
ChapterProvider.titlePaint drawChars(
} else { canvas,
ChapterProvider.contentPaint textLine.textChars,
} lineTop,
textPaint.color = if (textLine.isReadAloud) { lineBase,
context.accentColor lineBottom,
} else { textLine.isTitle,
ReadBookConfig.durConfig.textColor() textLine.isReadAloud
}
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
) )
} }
} }
private fun drawChars(
canvas: Canvas,
textChars: List<TextChar>,
lineTop: Float,
lineBase: Float,
lineBottom: Float,
isTitle: Boolean,
isReadAloud: Boolean
) {
val textPaint = if (isTitle) ChapterProvider.titlePaint else ChapterProvider.contentPaint
textPaint.color =
if (isReadAloud) context.accentColor else ReadBookConfig.durConfig.textColor()
textChars.forEach {
canvas.drawText(it.charData, it.start, lineBase, textPaint)
if (it.selected) {
canvas.drawRect(it.start, lineTop, it.end, lineBottom, selectedPaint)
} }
} }
} }
@ -208,17 +166,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
isLastPage = false isLastPage = false
} }
private fun switchToPageOffset(offset: Int) {
when (offset) {
1 -> {
}
-1 -> {
}
}
}
fun selectText(x: Float, y: Float): Boolean { fun selectText(x: Float, y: Float): Boolean {
for ((lineIndex, textLine) in textPage.textLines.withIndex()) { for ((lineIndex, textLine) in textPage.textLines.withIndex()) {
if (y > textLine.lineTop && y < textLine.lineBottom) { if (y > textLine.lineTop && y < textLine.lineBottom) {

Loading…
Cancel
Save