feat: 优化代码

pull/111/head
kunfei 5 years ago
parent 0f88499063
commit 57a472b411
  1. 36
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 63
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  3. 41
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt
  4. 7
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt
  5. 6
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt

@ -36,7 +36,6 @@ import io.legado.app.ui.book.read.page.ChapterProvider
import io.legado.app.ui.book.read.page.ContentTextView import io.legado.app.ui.book.read.page.ContentTextView
import io.legado.app.ui.book.read.page.PageView import io.legado.app.ui.book.read.page.PageView
import io.legado.app.ui.book.read.page.delegate.PageDelegate import io.legado.app.ui.book.read.page.delegate.PageDelegate
import io.legado.app.ui.book.read.page.entities.SelectPoint
import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.ui.book.source.edit.BookSourceEditActivity
import io.legado.app.ui.changesource.ChangeSourceDialog import io.legado.app.ui.changesource.ChangeSourceDialog
import io.legado.app.ui.chapterlist.ChapterListActivity import io.legado.app.ui.chapterlist.ChapterListActivity
@ -318,6 +317,20 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
override fun onTouch(v: View, event: MotionEvent): Boolean { override fun onTouch(v: View, event: MotionEvent): Boolean {
when (event.action) { when (event.action) {
MotionEvent.ACTION_DOWN -> {
when (v.id) {
R.id.cursor_left -> {
val startX = v.x + 10.dp
val startY = v.y - 10.dp
page_view.setSelectMoveStart(startX, startY)
}
R.id.cursor_right -> {
val startX = v.x - 10.dp
val startY = v.y - 10.dp
page_view.setSelectMoveStart(startX, startY)
}
}
}
MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP -> { MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP -> {
when (v.id) { when (v.id) {
R.id.cursor_left -> page_view.selectStartMove(event.x, event.y) R.id.cursor_left -> page_view.selectStartMove(event.x, event.y)
@ -328,6 +341,18 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
return true return true
} }
override fun upSelectedStart(x: Float, y: Float) {
cursor_left.x = x - cursor_left.width
cursor_left.y = y
cursor_left.visible()
}
override fun upSelectedEnd(x: Float, y: Float) {
cursor_right.x = x
cursor_right.y = y
cursor_right.visible()
}
override fun onCancelSelect() { override fun onCancelSelect() {
cursor_left.invisible() cursor_left.invisible()
cursor_right.invisible() cursor_right.invisible()
@ -418,15 +443,6 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
} }
} }
override fun selectText(selectPoint: SelectPoint) {
cursor_left.x = selectPoint.startX - cursor_left.width
cursor_left.y = selectPoint.startY
cursor_right.x = selectPoint.endX
cursor_right.y = selectPoint.endY
cursor_left.visible()
cursor_right.visible()
}
override fun showReadAloudDialog() { override fun showReadAloudDialog() {
ReadAloudDialog().show(supportFragmentManager, "readAloud") ReadAloudDialog().show(supportFragmentManager, "readAloud")
} }

@ -9,7 +9,6 @@ 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.ui.book.read.page.entities.SelectPoint
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
@ -24,6 +23,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
} }
private var activityCallBack: CallBack? = null private var activityCallBack: CallBack? = null
var headerHeight = 0
var selectAble = context.getPrefBoolean(PreferKey.textSelectAble) var selectAble = context.getPrefBoolean(PreferKey.textSelectAble)
var selectStartLine = 0 var selectStartLine = 0
var selectStartChar = 0 var selectStartChar = 0
@ -84,7 +84,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
} }
fun selectText(x: Float, y: Float): SelectPoint? { fun selectText(x: Float, y: Float): Boolean {
textPage?.let { textPage -> 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) {
@ -96,19 +96,22 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
selectStartChar = charIndex selectStartChar = charIndex
selectEndLine = lineIndex selectEndLine = lineIndex
selectEndChar = charIndex selectEndChar = charIndex
return SelectPoint( upSelectedStart(
textChar.leftBottomPosition.x, textChar.leftBottomPosition.x,
textChar.leftBottomPosition.y.toFloat(), textChar.leftBottomPosition.y.toFloat()
)
upSelectedEnd(
textChar.rightTopPosition.x, textChar.rightTopPosition.x,
textChar.leftBottomPosition.y.toFloat() textChar.leftBottomPosition.y.toFloat()
) )
return true
} }
} }
break break
} }
} }
} }
return null return false
} }
fun selectStartMove(x: Float, y: Float) { fun selectStartMove(x: Float, y: Float) {
@ -117,18 +120,15 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
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 if (selectStartLine != lineIndex || selectStartChar != charIndex) {
invalidate()
selectStartLine = lineIndex selectStartLine = lineIndex
selectStartChar = charIndex selectStartChar = charIndex
selectEndLine = lineIndex upSelectedStart(
selectEndChar = charIndex
SelectPoint(
textChar.leftBottomPosition.x, textChar.leftBottomPosition.x,
textChar.leftBottomPosition.y.toFloat(),
textChar.rightTopPosition.x,
textChar.leftBottomPosition.y.toFloat() textChar.leftBottomPosition.y.toFloat()
) )
upSelectChars(textPage)
}
break break
} }
} }
@ -144,18 +144,16 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
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 if (selectEndLine != lineIndex || selectEndChar != charIndex) {
invalidate()
selectStartLine = lineIndex
selectStartChar = charIndex
selectEndLine = lineIndex selectEndLine = lineIndex
selectEndChar = charIndex selectEndChar = charIndex
SelectPoint( upSelectedEnd(
textChar.leftBottomPosition.x,
textChar.leftBottomPosition.y.toFloat(),
textChar.rightTopPosition.x, textChar.rightTopPosition.x,
textChar.leftBottomPosition.y.toFloat() textChar.leftBottomPosition.y.toFloat()
) )
upSelectChars(textPage)
}
break
} }
} }
break break
@ -164,6 +162,31 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
} }
private fun upSelectChars(textPage: TextPage) {
for ((lineIndex, textLine) in textPage.textLines.withIndex()) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) {
textChar.selected = when (lineIndex) {
selectStartLine -> {
charIndex >= selectStartChar
}
selectEndLine -> {
charIndex <= selectEndChar
}
else -> lineIndex in (selectStartLine + 1) until selectEndLine
}
}
}
invalidate()
}
private fun upSelectedStart(x: Float, y: Float) {
activityCallBack?.upSelectedStart(x, y + headerHeight)
}
private fun upSelectedEnd(x: Float, y: Float) {
activityCallBack?.upSelectedEnd(x, y + headerHeight)
}
fun cancelSelect() { fun cancelSelect() {
textPage?.let { textPage -> textPage?.let { textPage ->
textPage.textLines.forEach { textLine -> textPage.textLines.forEach { textLine ->
@ -177,6 +200,8 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
interface CallBack { interface CallBack {
fun upSelectedStart(x: Float, y: Float)
fun upSelectedEnd(x: Float, y: Float)
fun onCancelSelect() fun onCancelSelect()
} }
} }

@ -3,34 +3,25 @@ package io.legado.app.ui.book.read.page
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.MotionEvent import android.view.MotionEvent
import android.widget.FrameLayout import android.widget.FrameLayout
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.AppConst.TIME_FORMAT import io.legado.app.constant.AppConst.TIME_FORMAT
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.ui.book.read.page.entities.SelectPoint
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.* import io.legado.app.utils.*
import kotlinx.android.synthetic.main.view_book_page.view.* import kotlinx.android.synthetic.main.view_book_page.view.*
import java.util.* import java.util.*
class ContentView : FrameLayout { class ContentView(context: Context) : FrameLayout(context) {
var callBack: CallBack? = null var callBack: CallBack? = null
private var headerHeight = 0
private var pageSize: Int = 0 private var pageSize: Int = 0
private var textSelectMoveStartX = 0f
private var textSelectMoveStartY = 0f
constructor(context: Context) : super(context) { init {
init()
}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
init()
}
fun init() {
//设置背景防止切换背景时文字重叠 //设置背景防止切换背景时文字重叠
setBackgroundColor(context.getCompatColor(R.color.background)) setBackgroundColor(context.getCompatColor(R.color.background))
inflate(context, R.layout.view_book_page, this) inflate(context, R.layout.view_book_page, this)
@ -55,12 +46,12 @@ class ContentView : FrameLayout {
headerPaddingRight.dp, headerPaddingRight.dp,
headerPaddingBottom.dp headerPaddingBottom.dp
) )
headerHeight = ll_header.height content_text_view.headerHeight = ll_header.height
page_panel.setPadding(0, 0, 0, 0) page_panel.setPadding(0, 0, 0, 0)
} else { } else {
ll_header.gone() ll_header.gone()
headerHeight = context.getStatusBarHeight() content_text_view.headerHeight = context.getStatusBarHeight()
page_panel.setPadding(0, headerHeight, 0, 0) page_panel.setPadding(0, content_text_view.headerHeight, 0, 0)
} }
content_text_view.setPadding( content_text_view.setPadding(
paddingLeft.dp, paddingLeft.dp,
@ -115,22 +106,22 @@ class ContentView : FrameLayout {
content_text_view.selectAble = selectAble content_text_view.selectAble = selectAble
} }
fun selectText(e: MotionEvent): SelectPoint? { fun selectText(e: MotionEvent): Boolean {
val y = e.y - headerHeight val y = e.y - content_text_view.headerHeight
val selectPoint = content_text_view.selectText(e.x, y) return content_text_view.selectText(e.x, y)
selectPoint?.let {
it.startY = it.startY + headerHeight
it.endY = it.endY + headerHeight
} }
return selectPoint
fun setSelectMoveStart(x: Float, y: Float) {
textSelectMoveStartX = x
textSelectMoveStartY = y - content_text_view.headerHeight
} }
fun selectStartMove(x: Float, y: Float) { fun selectStartMove(x: Float, y: Float) {
content_text_view.selectStartMove(x, y) content_text_view.selectStartMove(textSelectMoveStartX + x, textSelectMoveStartY + y)
} }
fun selectEndMove(x: Float, y: Float) { fun selectEndMove(x: Float, y: Float) {
content_text_view.selectEndMove(x, y) content_text_view.selectEndMove(textSelectMoveStartX + x, textSelectMoveStartY + y)
} }
fun cancelSelect() { fun cancelSelect() {

@ -10,7 +10,6 @@ import io.legado.app.constant.PreferKey
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.service.help.ReadBook import io.legado.app.service.help.ReadBook
import io.legado.app.ui.book.read.page.delegate.* import io.legado.app.ui.book.read.page.delegate.*
import io.legado.app.ui.book.read.page.entities.SelectPoint
import io.legado.app.ui.book.read.page.entities.TextChapter import io.legado.app.ui.book.read.page.entities.TextChapter
import io.legado.app.utils.activity import io.legado.app.utils.activity
import io.legado.app.utils.getPrefInt import io.legado.app.utils.getPrefInt
@ -184,6 +183,10 @@ class PageView(context: Context, attrs: AttributeSet) :
nextPage.upBattery(battery) nextPage.upBattery(battery)
} }
fun setSelectMoveStart(x: Float, y: Float) {
curPage.setSelectMoveStart(x, y)
}
fun selectStartMove(x: Float, y: Float) { fun selectStartMove(x: Float, y: Float) {
curPage.selectStartMove(x, y) curPage.selectStartMove(x, y)
} }
@ -263,7 +266,5 @@ class PageView(context: Context, attrs: AttributeSet) :
fun clickCenter() fun clickCenter()
fun screenOffTimerStart() fun screenOffTimerStart()
fun selectText(selectPoint: SelectPoint)
} }
} }

@ -276,11 +276,7 @@ abstract class PageDelegate(protected val pageView: PageView) :
* 长按选择 * 长按选择
*/ */
override fun onLongPress(e: MotionEvent) { override fun onLongPress(e: MotionEvent) {
val textChar = curPage.selectText(e) isTextSelected = curPage.selectText(e)
textChar?.let {
isTextSelected = true
pageView.callBack?.selectText(it)
}
} }
/** /**

Loading…
Cancel
Save