feat: 优化代码

pull/115/head
kunfei 5 years ago
parent d0ebf38999
commit d590cc186a
  1. 8
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt
  2. 28
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  3. 37
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt

@ -23,10 +23,10 @@ import io.legado.app.utils.removePref
object ChapterProvider {
var viewWidth = 0
var viewHeight = 0
private var visibleWidth = 0
private var visibleHeight = 0
private var paddingLeft = 0
private var paddingTop = 0
var visibleWidth = 0
var visibleHeight = 0
var paddingLeft = 0
var paddingTop = 0
private var lineSpacingExtra = 0f
private var paragraphSpacing = 0
var typeface: Typeface = Typeface.SANS_SERIF

@ -3,8 +3,6 @@ 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
@ -30,7 +28,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
private var selectCharStart = 0
private var selectLineEnd = 0
private var selectCharEnd = 0
private var textPage: TextPage = TextPage()
private var textPage: TextPage = TextPage().textToLine()
//滚动参数
private val pageFactory: TextPageFactory get() = callBack.pageFactory
private val maxScrollOffset = 100f
@ -60,29 +58,13 @@ 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)
if (ReadBookConfig.isScroll) {
drawScrollPage(canvas)
} 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) {
@ -110,6 +92,10 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
}
}
private fun drawScrollPage(canvas: Canvas) {
}
fun onScroll(mOffset: Float) {
var offset = mOffset
if (offset > maxScrollOffset) {

@ -1,7 +1,10 @@
package io.legado.app.ui.book.read.page.entities
import android.text.Layout
import android.text.StaticLayout
import io.legado.app.App
import io.legado.app.R
import io.legado.app.ui.book.read.page.ChapterProvider
data class TextPage(
var index: Int = 0,
@ -14,6 +17,40 @@ data class TextPage(
var height: Int = 0
) {
@Suppress("DEPRECATION")
fun textToLine(): TextPage {
if (textLines.isEmpty()) {
val layout = StaticLayout(
text, ChapterProvider.contentPaint, ChapterProvider.visibleWidth,
Layout.Alignment.ALIGN_NORMAL, 1f, 0f, false
)
var y = (ChapterProvider.visibleHeight - layout.height) / 2f
if (y < 0) y = 0f
for (lineIndex in 0 until layout.lineCount) {
val textLine = TextLine()
textLine.lineTop = (ChapterProvider.paddingTop + y -
(layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex)))
textLine.lineBase = (ChapterProvider.paddingTop + y -
(layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex)))
textLine.lineBottom =
textLine.lineBase + ChapterProvider.contentPaint.fontMetrics.descent
var x = (ChapterProvider.visibleWidth - layout.getLineMax(lineIndex)) / 2
textLine.text =
text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex))
for (i in textLine.text.indices) {
val char = textLine.text[i].toString()
val cw = StaticLayout.getDesiredWidth(char, ChapterProvider.contentPaint)
val x1 = x + cw
textLine.textChars.add(TextChar(charData = char, start = x, end = x1))
x = x1
}
textLines.add(textLine)
}
height = ChapterProvider.visibleHeight
}
return this
}
fun removePageAloudSpan(): TextPage {
textLines.forEach { textLine ->
textLine.isReadAloud = false

Loading…
Cancel
Save