feat: 优化代码

pull/111/head
kunfei 5 years ago
parent 6294c92982
commit 68fc545cfe
  1. 16
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt
  2. 35
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  3. 8
      app/src/main/java/io/legado/app/ui/book/read/page/entities/SelectPoint.kt
  4. 4
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt
  5. 3
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt
  6. 2
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPoint.kt

@ -166,10 +166,10 @@ object ChapterProvider {
textPages.add(TextPage())
textPages.last().textLines.add(textLine)
}
textLine.lineBottom = paddingTop + durY -
(layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex))
textLine.lineTop = paddingTop + durY -
(layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex))
textLine.lineBottom = (paddingTop + durY -
(layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex))).toFloat()
textLine.lineTop = (paddingTop + durY -
(layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex))).toFloat()
val words =
title.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex))
stringBuilder.append(words)
@ -246,10 +246,10 @@ object ChapterProvider {
textPages.add(TextPage())
textPages.last().textLines.add(textLine)
}
textLine.lineBottom = paddingTop + durY -
(layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex))
textLine.lineTop = paddingTop + durY -
(layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex))
textLine.lineBottom = (paddingTop + durY -
(layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex))).toFloat()
textLine.lineTop = (paddingTop + durY -
(layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex))).toFloat()
var words =
text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex))
stringBuilder.append(words)

@ -9,6 +9,7 @@ import io.legado.app.R
import io.legado.app.constant.PreferKey
import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.theme.accentColor
import io.legado.app.service.help.ReadBook
import io.legado.app.ui.book.read.page.entities.TextPage
import io.legado.app.utils.activity
import io.legado.app.utils.getCompatColor
@ -31,8 +32,8 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
private var textPage: TextPage? = null
//滚动参数
private val maxScrollOffset = 100f
private val pageOffset = 0f
private val isLastPage = false
private var pageOffset = 0f
private var isLastPage = false
init {
activityCallBack = activity as CallBack
@ -70,15 +71,15 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
canvas.drawText(
it.charData,
it.leftBottomPosition.x,
it.leftBottomPosition.y.toFloat(),
it.leftBottomPosition.y,
textPaint
)
if (it.selected) {
canvas.drawRect(
it.leftBottomPosition.x,
it.rightTopPosition.y.toFloat(),
it.rightTopPosition.y,
it.rightTopPosition.x,
it.leftBottomPosition.y.toFloat(),
it.leftBottomPosition.y,
selectedPaint
)
}
@ -87,14 +88,22 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
}
}
fun onScroll(offset: Float) {
var mOffset = offset
fun onScroll(mOffset: Float) {
var offset = mOffset
if (offset > maxScrollOffset) {
mOffset = maxScrollOffset
offset = maxScrollOffset
} else if (offset < -maxScrollOffset) {
mOffset = -maxScrollOffset
offset = -maxScrollOffset
}
if (!isLastPage || offset < 0) {
pageOffset += offset
isLastPage = false
}
// 首页
if (pageOffset < 0 && ReadBook.durChapterIndex == 0 && ReadBook.durPageIndex == 0) {
pageOffset = 0f
}
}
@ -112,11 +121,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
selectCharEnd = charIndex
upSelectedStart(
textChar.leftBottomPosition.x,
textChar.leftBottomPosition.y.toFloat()
textChar.leftBottomPosition.y
)
upSelectedEnd(
textChar.rightTopPosition.x,
textChar.leftBottomPosition.y.toFloat()
textChar.leftBottomPosition.y
)
return true
}
@ -139,7 +148,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
selectCharStart = charIndex
upSelectedStart(
textChar.leftBottomPosition.x,
textChar.leftBottomPosition.y.toFloat()
textChar.leftBottomPosition.y
)
upSelectChars(textPage)
}
@ -163,7 +172,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
selectCharEnd = charIndex
upSelectedEnd(
textChar.rightTopPosition.x,
textChar.leftBottomPosition.y.toFloat()
textChar.leftBottomPosition.y
)
upSelectChars(textPage)
}

@ -1,8 +0,0 @@
package io.legado.app.ui.book.read.page.entities
data class SelectPoint(
var startX: Float,
var startY: Float,
var endX: Float,
var endY: Float
)

@ -3,8 +3,8 @@ package io.legado.app.ui.book.read.page.entities
data class TextLine(
var text: String = "",
val textChars: ArrayList<TextChar> = arrayListOf(),
var lineTop: Int = 0,
var lineBottom: Int = 0,
var lineTop: Float = 0f,
var lineBottom: Float = 0f,
val isTitle: Boolean = false,
var isReadAloud: Boolean = false
)

@ -10,7 +10,8 @@ data class TextPage(
val textLines: ArrayList<TextLine> = arrayListOf(),
var pageSize: Int = 0,
var chapterSize: Int = 0,
var chapterIndex: Int = 0
var chapterIndex: Int = 0,
var height: Int = 0
) {
fun removePageAloudSpan(): TextPage {

@ -2,5 +2,5 @@ package io.legado.app.ui.book.read.page.entities
data class TextPoint(
val x: Float,
val y: Int
val y: Float
)
Loading…
Cancel
Save