From 57a472b41114f19a2a54ee12af70ae7bcf783258 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 22 Feb 2020 09:27:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/read/ReadBookActivity.kt | 36 +++++--- .../app/ui/book/read/page/ContentTextView.kt | 83 ++++++++++++------- .../app/ui/book/read/page/ContentView.kt | 43 ++++------ .../legado/app/ui/book/read/page/PageView.kt | 7 +- .../book/read/page/delegate/PageDelegate.kt | 6 +- 5 files changed, 102 insertions(+), 73 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index b890d01e3..47a087d30 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.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.PageView 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.changesource.ChangeSourceDialog import io.legado.app.ui.chapterlist.ChapterListActivity @@ -318,6 +317,20 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo @SuppressLint("ClickableViewAccessibility") override fun onTouch(v: View, event: MotionEvent): Boolean { 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 -> { when (v.id) { R.id.cursor_left -> page_view.selectStartMove(event.x, event.y) @@ -328,6 +341,18 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo 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() { cursor_left.invisible() cursor_right.invisible() @@ -418,15 +443,6 @@ class ReadBookActivity : VMBaseActivity(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() { ReadAloudDialog().show(supportFragmentManager, "readAloud") } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt index 1265c3ec2..fd65c0814 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt @@ -9,7 +9,6 @@ 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.ui.book.read.page.entities.SelectPoint import io.legado.app.ui.book.read.page.entities.TextPage import io.legado.app.utils.activity import io.legado.app.utils.getCompatColor @@ -24,6 +23,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at } } private var activityCallBack: CallBack? = null + var headerHeight = 0 var selectAble = context.getPrefBoolean(PreferKey.textSelectAble) var selectStartLine = 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 -> for ((lineIndex, textLine) in textPage.textLines.withIndex()) { if (y > textLine.lineTop && y < textLine.lineBottom) { @@ -96,19 +96,22 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at selectStartChar = charIndex selectEndLine = lineIndex selectEndChar = charIndex - return SelectPoint( + upSelectedStart( textChar.leftBottomPosition.x, - textChar.leftBottomPosition.y.toFloat(), + textChar.leftBottomPosition.y.toFloat() + ) + upSelectedEnd( textChar.rightTopPosition.x, textChar.leftBottomPosition.y.toFloat() ) + return true } } break } } } - return null + return false } 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) { for ((charIndex, textChar) in textLine.textChars.withIndex()) { if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) { - textChar.selected = true - invalidate() - selectStartLine = lineIndex - selectStartChar = charIndex - selectEndLine = lineIndex - selectEndChar = charIndex - SelectPoint( - textChar.leftBottomPosition.x, - textChar.leftBottomPosition.y.toFloat(), - textChar.rightTopPosition.x, - textChar.leftBottomPosition.y.toFloat() - ) + if (selectStartLine != lineIndex || selectStartChar != charIndex) { + selectStartLine = lineIndex + selectStartChar = charIndex + upSelectedStart( + textChar.leftBottomPosition.x, + textChar.leftBottomPosition.y.toFloat() + ) + upSelectChars(textPage) + } break } } @@ -144,18 +144,16 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at if (y > textLine.lineTop && y < textLine.lineBottom) { for ((charIndex, textChar) in textLine.textChars.withIndex()) { if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) { - textChar.selected = true - invalidate() - selectStartLine = lineIndex - selectStartChar = charIndex - selectEndLine = lineIndex - selectEndChar = charIndex - SelectPoint( - textChar.leftBottomPosition.x, - textChar.leftBottomPosition.y.toFloat(), - textChar.rightTopPosition.x, - textChar.leftBottomPosition.y.toFloat() - ) + if (selectEndLine != lineIndex || selectEndChar != charIndex) { + selectEndLine = lineIndex + selectEndChar = charIndex + upSelectedEnd( + textChar.rightTopPosition.x, + textChar.leftBottomPosition.y.toFloat() + ) + upSelectChars(textPage) + } + 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() { textPage?.let { textPage -> textPage.textLines.forEach { textLine -> @@ -177,6 +200,8 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at } interface CallBack { + fun upSelectedStart(x: Float, y: Float) + fun upSelectedEnd(x: Float, y: Float) fun onCancelSelect() } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt index 089a31bd6..bf617a639 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt @@ -3,34 +3,25 @@ package io.legado.app.ui.book.read.page import android.annotation.SuppressLint import android.content.Context import android.graphics.drawable.Drawable -import android.util.AttributeSet import android.view.MotionEvent import android.widget.FrameLayout import io.legado.app.R import io.legado.app.constant.AppConst.TIME_FORMAT import io.legado.app.constant.PreferKey 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.utils.* import kotlinx.android.synthetic.main.view_book_page.view.* import java.util.* -class ContentView : FrameLayout { +class ContentView(context: Context) : FrameLayout(context) { var callBack: CallBack? = null - private var headerHeight = 0 private var pageSize: Int = 0 + private var textSelectMoveStartX = 0f + private var textSelectMoveStartY = 0f - constructor(context: Context) : super(context) { - init() - } - - constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { - init() - } - - fun init() { + init { //设置背景防止切换背景时文字重叠 setBackgroundColor(context.getCompatColor(R.color.background)) inflate(context, R.layout.view_book_page, this) @@ -55,12 +46,12 @@ class ContentView : FrameLayout { headerPaddingRight.dp, headerPaddingBottom.dp ) - headerHeight = ll_header.height + content_text_view.headerHeight = ll_header.height page_panel.setPadding(0, 0, 0, 0) } else { ll_header.gone() - headerHeight = context.getStatusBarHeight() - page_panel.setPadding(0, headerHeight, 0, 0) + content_text_view.headerHeight = context.getStatusBarHeight() + page_panel.setPadding(0, content_text_view.headerHeight, 0, 0) } content_text_view.setPadding( paddingLeft.dp, @@ -115,22 +106,22 @@ class ContentView : FrameLayout { content_text_view.selectAble = selectAble } - fun selectText(e: MotionEvent): SelectPoint? { - val y = e.y - headerHeight - val selectPoint = content_text_view.selectText(e.x, y) - selectPoint?.let { - it.startY = it.startY + headerHeight - it.endY = it.endY + headerHeight - } - return selectPoint + fun selectText(e: MotionEvent): Boolean { + val y = e.y - content_text_view.headerHeight + return content_text_view.selectText(e.x, y) + } + + fun setSelectMoveStart(x: Float, y: Float) { + textSelectMoveStartX = x + textSelectMoveStartY = y - content_text_view.headerHeight } 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) { - content_text_view.selectEndMove(x, y) + content_text_view.selectEndMove(textSelectMoveStartX + x, textSelectMoveStartY + y) } fun cancelSelect() { diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt index 13a3322d2..f2bdd54d6 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt @@ -10,7 +10,6 @@ import io.legado.app.constant.PreferKey import io.legado.app.help.ReadBookConfig import io.legado.app.service.help.ReadBook 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.utils.activity import io.legado.app.utils.getPrefInt @@ -184,6 +183,10 @@ class PageView(context: Context, attrs: AttributeSet) : nextPage.upBattery(battery) } + fun setSelectMoveStart(x: Float, y: Float) { + curPage.setSelectMoveStart(x, y) + } + fun selectStartMove(x: Float, y: Float) { curPage.selectStartMove(x, y) } @@ -263,7 +266,5 @@ class PageView(context: Context, attrs: AttributeSet) : fun clickCenter() fun screenOffTimerStart() - - fun selectText(selectPoint: SelectPoint) } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt index 4cb163900..814cfd15c 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt @@ -276,11 +276,7 @@ abstract class PageDelegate(protected val pageView: PageView) : * 长按选择 */ override fun onLongPress(e: MotionEvent) { - val textChar = curPage.selectText(e) - textChar?.let { - isTextSelected = true - pageView.callBack?.selectText(it) - } + isTextSelected = curPage.selectText(e) } /**