feat: 优化代码

pull/111/head
kunfei 5 years ago
parent f77fe68745
commit f06d77de89
  1. 199
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt

@ -29,7 +29,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
private var selectCharStart = 0 private var selectCharStart = 0
private var selectLineEnd = 0 private var selectLineEnd = 0
private var selectCharEnd = 0 private var selectCharEnd = 0
private var textPage: TextPage? = null private var textPage: TextPage = TextPage()
//滚动参数 //滚动参数
private val maxScrollOffset = 100f private val maxScrollOffset = 100f
private var pageOffset = 0f private var pageOffset = 0f
@ -39,7 +39,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
activityCallBack = activity as CallBack activityCallBack = activity as CallBack
} }
fun setContent(textPage: TextPage?) { fun setContent(textPage: TextPage) {
this.textPage = textPage this.textPage = textPage
invalidate() invalidate()
} }
@ -55,34 +55,32 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
super.onDraw(canvas) super.onDraw(canvas)
textPage?.let { textPage -> textPage.textLines.forEach { textLine ->
textPage.textLines.forEach { textLine -> val textPaint = if (textLine.isTitle) {
val textPaint = if (textLine.isTitle) { ChapterProvider.titlePaint
ChapterProvider.titlePaint } else {
} else { ChapterProvider.contentPaint
ChapterProvider.contentPaint }
} textPaint.color = if (textLine.isReadAloud) {
textPaint.color = if (textLine.isReadAloud) { context.accentColor
context.accentColor } else {
} else { ReadBookConfig.durConfig.textColor()
ReadBookConfig.durConfig.textColor() }
} textLine.textChars.forEach {
textLine.textChars.forEach { canvas.drawText(
canvas.drawText( it.charData,
it.charData, it.leftBottomPosition.x,
it.leftBottomPosition.y,
textPaint
)
if (it.selected) {
canvas.drawRect(
it.leftBottomPosition.x, it.leftBottomPosition.x,
it.rightTopPosition.y,
it.rightTopPosition.x,
it.leftBottomPosition.y, it.leftBottomPosition.y,
textPaint selectedPaint
) )
if (it.selected) {
canvas.drawRect(
it.leftBottomPosition.x,
it.rightTopPosition.y,
it.rightTopPosition.x,
it.leftBottomPosition.y,
selectedPaint
)
}
} }
} }
} }
@ -105,82 +103,77 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
pageOffset = 0f pageOffset = 0f
} }
} }
fun selectText(x: Float, y: Float): Boolean { fun selectText(x: Float, y: Float): Boolean {
textPage?.let { textPage -> 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) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) {
if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) { textChar.selected = true
textChar.selected = true invalidate()
invalidate() selectLineStart = lineIndex
selectLineStart = lineIndex selectCharStart = charIndex
selectCharStart = charIndex selectLineEnd = lineIndex
selectLineEnd = lineIndex selectCharEnd = charIndex
selectCharEnd = charIndex upSelectedStart(
upSelectedStart( textChar.leftBottomPosition.x,
textChar.leftBottomPosition.x, textChar.leftBottomPosition.y
textChar.leftBottomPosition.y )
) upSelectedEnd(
upSelectedEnd( textChar.rightTopPosition.x,
textChar.rightTopPosition.x, textChar.leftBottomPosition.y
textChar.leftBottomPosition.y )
) return true
return true
}
} }
break
} }
break
} }
} }
return false return false
} }
fun selectStartMove(x: Float, y: Float) { fun selectStartMove(x: Float, y: Float) {
textPage?.let { textPage -> 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) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) {
if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) { if (selectLineStart != lineIndex || selectCharStart != charIndex) {
if (selectLineStart != lineIndex || selectCharStart != charIndex) { selectLineStart = lineIndex
selectLineStart = lineIndex selectCharStart = charIndex
selectCharStart = charIndex upSelectedStart(
upSelectedStart( textChar.leftBottomPosition.x,
textChar.leftBottomPosition.x, textChar.leftBottomPosition.y
textChar.leftBottomPosition.y )
) upSelectChars(textPage)
upSelectChars(textPage)
}
break
} }
break
} }
break
} }
break
} }
} }
} }
fun selectEndMove(x: Float, y: Float) { fun selectEndMove(x: Float, y: Float) {
textPage?.let { textPage -> 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) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) {
if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) { if (selectLineEnd != lineIndex || selectCharEnd != charIndex) {
if (selectLineEnd != lineIndex || selectCharEnd != charIndex) { selectLineEnd = lineIndex
selectLineEnd = lineIndex selectCharEnd = charIndex
selectCharEnd = charIndex upSelectedEnd(
upSelectedEnd( textChar.rightTopPosition.x,
textChar.rightTopPosition.x, textChar.leftBottomPosition.y
textChar.leftBottomPosition.y )
) upSelectChars(textPage)
upSelectChars(textPage)
}
break
} }
break
} }
break
} }
break
} }
} }
} }
@ -212,38 +205,38 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
fun cancelSelect() { fun cancelSelect() {
textPage?.let { textPage -> textPage.textLines.forEach { textLine ->
textPage.textLines.forEach { textLine -> textLine.textChars.forEach {
textLine.textChars.forEach { it.selected = false
it.selected = false
}
} }
invalidate()
} }
invalidate()
activityCallBack.onCancelSelect() activityCallBack.onCancelSelect()
} }
val selectedText: String val selectedText: String
get() { get() {
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
textPage?.let { for (lineIndex in selectLineStart..selectLineEnd) {
for (lineIndex in selectLineStart..selectLineEnd) { if (lineIndex == selectLineStart && lineIndex == selectLineEnd) {
if (lineIndex == selectLineStart && lineIndex == selectLineEnd) { stringBuilder.append(
stringBuilder.append( textPage.textLines[lineIndex].text.substring(
it.textLines[lineIndex].text.substring( selectCharStart,
selectCharStart, selectCharEnd + 1
selectCharEnd + 1
)
) )
} else if (lineIndex == selectLineStart) { )
stringBuilder.append(it.textLines[lineIndex].text.substring(selectCharStart)) } else if (lineIndex == selectLineStart) {
} else if (lineIndex == selectLineEnd) { stringBuilder.append(
stringBuilder.append( textPage.textLines[lineIndex].text.substring(
it.textLines[lineIndex].text.substring(0, selectCharEnd + 1) selectCharStart
) )
} else { )
stringBuilder.append(it.textLines[lineIndex].text) } else if (lineIndex == selectLineEnd) {
} stringBuilder.append(
textPage.textLines[lineIndex].text.substring(0, selectCharEnd + 1)
)
} else {
stringBuilder.append(textPage.textLines[lineIndex].text)
} }
} }
return stringBuilder.toString() return stringBuilder.toString()

@ -93,8 +93,8 @@ class ContentView(context: Context) : FrameLayout(context) {
} }
fun setContent(textPage: TextPage?) { fun setContent(textPage: TextPage?) {
content_text_view.setContent(textPage)
if (textPage != null) { if (textPage != null) {
content_text_view.setContent(textPage)
tv_bottom_left.text = textPage.title tv_bottom_left.text = textPage.title
pageSize = textPage.pageSize pageSize = textPage.pageSize
setPageIndex(textPage.index) setPageIndex(textPage.index)

Loading…
Cancel
Save