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