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

@ -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.service.help.ReadBook
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
@ -31,8 +32,8 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
private var textPage: TextPage? = null private var textPage: TextPage? = null
//滚动参数 //滚动参数
private val maxScrollOffset = 100f private val maxScrollOffset = 100f
private val pageOffset = 0f private var pageOffset = 0f
private val isLastPage = false private var isLastPage = false
init { init {
activityCallBack = activity as CallBack activityCallBack = activity as CallBack
@ -70,15 +71,15 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
canvas.drawText( canvas.drawText(
it.charData, it.charData,
it.leftBottomPosition.x, it.leftBottomPosition.x,
it.leftBottomPosition.y.toFloat(), it.leftBottomPosition.y,
textPaint textPaint
) )
if (it.selected) { if (it.selected) {
canvas.drawRect( canvas.drawRect(
it.leftBottomPosition.x, it.leftBottomPosition.x,
it.rightTopPosition.y.toFloat(), it.rightTopPosition.y,
it.rightTopPosition.x, it.rightTopPosition.x,
it.leftBottomPosition.y.toFloat(), it.leftBottomPosition.y,
selectedPaint selectedPaint
) )
} }
@ -87,14 +88,22 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
} }
fun onScroll(offset: Float) { fun onScroll(mOffset: Float) {
var mOffset = offset var offset = mOffset
if (offset > maxScrollOffset) { if (offset > maxScrollOffset) {
mOffset = maxScrollOffset offset = maxScrollOffset
} else if (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 selectCharEnd = charIndex
upSelectedStart( upSelectedStart(
textChar.leftBottomPosition.x, textChar.leftBottomPosition.x,
textChar.leftBottomPosition.y.toFloat() textChar.leftBottomPosition.y
) )
upSelectedEnd( upSelectedEnd(
textChar.rightTopPosition.x, textChar.rightTopPosition.x,
textChar.leftBottomPosition.y.toFloat() textChar.leftBottomPosition.y
) )
return true return true
} }
@ -139,7 +148,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
selectCharStart = charIndex selectCharStart = charIndex
upSelectedStart( upSelectedStart(
textChar.leftBottomPosition.x, textChar.leftBottomPosition.x,
textChar.leftBottomPosition.y.toFloat() textChar.leftBottomPosition.y
) )
upSelectChars(textPage) upSelectChars(textPage)
} }
@ -163,7 +172,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
selectCharEnd = charIndex selectCharEnd = charIndex
upSelectedEnd( upSelectedEnd(
textChar.rightTopPosition.x, textChar.rightTopPosition.x,
textChar.leftBottomPosition.y.toFloat() textChar.leftBottomPosition.y
) )
upSelectChars(textPage) 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( data class TextLine(
var text: String = "", var text: String = "",
val textChars: ArrayList<TextChar> = arrayListOf(), val textChars: ArrayList<TextChar> = arrayListOf(),
var lineTop: Int = 0, var lineTop: Float = 0f,
var lineBottom: Int = 0, var lineBottom: Float = 0f,
val isTitle: Boolean = false, val isTitle: Boolean = false,
var isReadAloud: Boolean = false var isReadAloud: Boolean = false
) )

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

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