feat: 优化代码

pull/115/head
kunfei 5 years ago
parent 2ee5376abc
commit 2864bc28ea
  1. 199
      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 textLine.lineTop,
} textLine.lineBase,
textPaint.color = if (textLine.isReadAloud) { textLine.lineBottom,
context.accentColor textLine.isTitle,
} else { textLine.isReadAloud
ReadBookConfig.durConfig.textColor() )
}
textLine.textChars.forEach {
canvas.drawText(
it.charData,
it.start,
textLine.lineBase,
textPaint
)
if (it.selected) {
canvas.drawRect(
it.start,
textLine.lineTop,
it.end,
textLine.lineBottom,
selectedPaint
)
}
}
} }
} }
private fun drawScrollPage(canvas: Canvas) { private fun drawScrollPage(canvas: Canvas) {
if (pageOffset <= 0) { textPage.textLines.forEach { textLine ->
textPage.textLines.forEach { textLine -> val yPy = pageOffset
val textPaint = if (textLine.isTitle) { val lineTop = textLine.lineTop + yPy
ChapterProvider.titlePaint val lineBase = textLine.lineBase + yPy
} else { val lineBottom = textLine.lineBottom + yPy
ChapterProvider.contentPaint drawChars(
} canvas,
textPaint.color = if (textLine.isReadAloud) { textLine.textChars,
context.accentColor lineTop,
} else { lineBase,
ReadBookConfig.durConfig.textColor() lineBottom,
} textLine.isTitle,
textLine.textChars.forEach { textLine.isReadAloud
canvas.drawText( )
it.charData, }
it.start, pageFactory.nextPage?.textLines?.forEach { textLine ->
textLine.lineBase + pageOffset, val yPy = pageOffset + textPage.height - ChapterProvider.paddingTop
textPaint val lineTop = textLine.lineTop + yPy
) val lineBase = textLine.lineBase + yPy
if (it.selected) { val lineBottom = textLine.lineBottom + yPy
canvas.drawRect( drawChars(
it.start, canvas,
textLine.lineTop + pageOffset, textLine.textChars,
it.end, lineTop,
textLine.lineBottom + pageOffset, lineBase,
selectedPaint lineBottom,
) textLine.isTitle,
} textLine.isReadAloud
} )
} }
pageFactory.nextPage?.textLines?.forEach { textLine -> pageFactory.prevPage?.textLines?.forEach { textLine ->
val textPaint = if (textLine.isTitle) { val yPy = pageOffset + 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
) private fun drawChars(
if (it.selected) { canvas: Canvas,
canvas.drawRect( textChars: List<TextChar>,
it.start, lineTop: Float,
textLine.lineTop + pageOffset + textPage.height - ChapterProvider.paddingTop, lineBase: Float,
it.end, lineBottom: Float,
textLine.lineBottom + pageOffset + textPage.height - ChapterProvider.paddingTop, isTitle: Boolean,
selectedPaint isReadAloud: Boolean
) ) {
} val textPaint = if (isTitle) ChapterProvider.titlePaint else ChapterProvider.contentPaint
} textPaint.color =
} if (isReadAloud) context.accentColor else ReadBookConfig.durConfig.textColor()
} else { textChars.forEach {
textPage.textLines.forEach { textLine -> canvas.drawText(it.charData, it.start, lineBase, textPaint)
val textPaint = if (textLine.isTitle) { if (it.selected) {
ChapterProvider.titlePaint canvas.drawRect(it.start, lineTop, it.end, lineBottom, selectedPaint)
} 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
)
}
}
} }
} }
} }
@ -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