feat: 优化代码

pull/117/head
kunfei 5 years ago
parent 2fec158533
commit 9ea581aabf
  1. 28
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  2. 12
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt
  3. 17
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt
  4. 9
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt

@ -176,7 +176,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
linePos = 0 linePos = 0
} }
fun selectText(x: Float, y: Float): Boolean { fun selectText(x: Float, y: Float, select: (lineIndex: Int, charIndex: Int) -> Unit) {
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()) {
@ -189,13 +189,12 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
selectCharEnd = charIndex selectCharEnd = charIndex
upSelectedStart(textChar.start, textLine.lineBottom) upSelectedStart(textChar.start, textLine.lineBottom)
upSelectedEnd(textChar.end, textLine.lineBottom) upSelectedEnd(textChar.end, textLine.lineBottom)
return true select(lineIndex, charIndex)
} }
} }
break break
} }
} }
return false
} }
fun selectStartMove(x: Float, y: Float) { fun selectStartMove(x: Float, y: Float) {
@ -217,6 +216,15 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
} }
fun selectStartMoveIndex(lineIndex: Int, charIndex: Int) {
selectLineStart = lineIndex
selectCharStart = charIndex
val textLine = textPage.textLines[lineIndex]
val textChar = textLine.textChars[charIndex]
upSelectedStart(textChar.start, textLine.lineBottom)
upSelectChars(textPage)
}
fun selectEndMove(x: Float, y: Float) { fun selectEndMove(x: Float, y: Float) {
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) {
@ -225,10 +233,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (selectLineEnd != lineIndex || selectCharEnd != charIndex) { if (selectLineEnd != lineIndex || selectCharEnd != charIndex) {
selectLineEnd = lineIndex selectLineEnd = lineIndex
selectCharEnd = charIndex selectCharEnd = charIndex
upSelectedEnd( upSelectedEnd(textChar.end, textLine.lineBottom)
textChar.end,
textLine.lineBottom
)
upSelectChars(textPage) upSelectChars(textPage)
} }
break break
@ -239,6 +244,15 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
} }
fun selectEndMoveIndex(lineIndex: Int, charIndex: Int) {
selectLineEnd = lineIndex
selectCharEnd = charIndex
val textLine = textPage.textLines[lineIndex]
val textChar = textLine.textChars[charIndex]
upSelectedEnd(textChar.end, textLine.lineBottom)
upSelectChars(textPage)
}
private fun upSelectChars(textPage: TextPage) { private fun upSelectChars(textPage: TextPage) {
for ((lineIndex, textLine) in textPage.textLines.withIndex()) { for ((lineIndex, textLine) in textPage.textLines.withIndex()) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {

@ -120,19 +120,27 @@ class ContentView(context: Context) : FrameLayout(context) {
content_text_view.selectAble = selectAble content_text_view.selectAble = selectAble
} }
fun selectText(e: MotionEvent): Boolean { fun selectText(e: MotionEvent, select: (lineIndex: Int, charIndex: Int) -> Unit) {
val y = e.y - headerHeight val y = e.y - headerHeight
return content_text_view.selectText(e.x, y) return content_text_view.selectText(e.x, y, select)
} }
fun selectStartMove(x: Float, y: Float) { fun selectStartMove(x: Float, y: Float) {
content_text_view.selectStartMove(x, y - headerHeight) content_text_view.selectStartMove(x, y - headerHeight)
} }
fun selectStartMoveIndex(lineIndex: Int, charIndex: Int) {
content_text_view.selectStartMoveIndex(lineIndex, charIndex)
}
fun selectEndMove(x: Float, y: Float) { fun selectEndMove(x: Float, y: Float) {
content_text_view.selectEndMove(x, y - headerHeight) content_text_view.selectEndMove(x, y - headerHeight)
} }
fun selectEndMoveIndex(lineIndex: Int, charIndex: Int) {
content_text_view.selectEndMoveIndex(lineIndex, charIndex)
}
fun cancelSelect() { fun cancelSelect() {
content_text_view.cancelSelect() content_text_view.cancelSelect()
} }

@ -1,6 +1,5 @@
package io.legado.app.ui.book.read.page.delegate package io.legado.app.ui.book.read.page.delegate
import android.util.Log
import android.view.MotionEvent import android.view.MotionEvent
import io.legado.app.ui.book.read.page.PageView import io.legado.app.ui.book.read.page.PageView
import kotlin.math.abs import kotlin.math.abs
@ -11,7 +10,7 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
when (event.action) { when (event.action) {
MotionEvent.ACTION_MOVE -> { MotionEvent.ACTION_MOVE -> {
if (isTextSelected) { if (isTextSelected) {
Log.e("selected", "--------------------------") selectText(event)
} else { } else {
scroll(event) scroll(event)
} }
@ -51,4 +50,18 @@ abstract class HorizontalPageDelegate(pageView: PageView) : PageDelegate(pageVie
setTouchPoint(event.x, event.y) setTouchPoint(event.x, event.y)
} }
} }
private fun selectText(event: MotionEvent) {
curPage.selectText(event) { lineIndex, charIndex ->
if (lineIndex > firstLineIndex
|| (lineIndex == firstLineIndex && charIndex > firstCharIndex)
) {
curPage.selectStartMoveIndex(firstLineIndex, firstCharIndex)
curPage.selectEndMoveIndex(lineIndex, charIndex)
} else {
curPage.selectEndMoveIndex(firstLineIndex, firstCharIndex)
curPage.selectStartMoveIndex(lineIndex, charIndex)
}
}
}
} }

@ -67,6 +67,9 @@ abstract class PageDelegate(protected val pageView: PageView) :
var isTextSelected = false var isTextSelected = false
var selectedOnDown = false var selectedOnDown = false
var firstLineIndex: Int = 0
var firstCharIndex: Int = 0
open fun setStartPoint(x: Float, y: Float, invalidate: Boolean = true) { open fun setStartPoint(x: Float, y: Float, invalidate: Boolean = true) {
startX = x startX = x
startY = y startY = y
@ -283,7 +286,11 @@ abstract class PageDelegate(protected val pageView: PageView) :
* 长按选择 * 长按选择
*/ */
override fun onLongPress(e: MotionEvent) { override fun onLongPress(e: MotionEvent) {
isTextSelected = curPage.selectText(e) curPage.selectText(e) { lineIndex, charIndex ->
isTextSelected = true
firstLineIndex = lineIndex
firstCharIndex = charIndex
}
} }
/** /**

Loading…
Cancel
Save