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 9e4b37673..e34a4e624 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 @@ -53,7 +53,6 @@ import io.legado.app.ui.replace.ReplaceRuleActivity import io.legado.app.ui.replace.edit.ReplaceEditActivity import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.utils.* -import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -109,7 +108,6 @@ class ReadBookActivity : ReadBookBaseActivity(), TextActionMenu(this, this) } - override val scope: CoroutineScope get() = lifecycleScope override val isInitFinish: Boolean get() = viewModel.isInitFinish override val isScroll: Boolean get() = binding.readView.isScroll private val mHandler = Handler(Looper.getMainLooper()) 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 970ec18ed..daa7b87d8 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 @@ -5,7 +5,6 @@ import android.graphics.Canvas import android.graphics.Paint import android.graphics.RectF import android.util.AttributeSet -import android.util.Log import android.view.View import io.legado.app.R import io.legado.app.constant.PreferKey @@ -24,7 +23,6 @@ import io.legado.app.utils.activity import io.legado.app.utils.getCompatColor import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.toastOnUi -import kotlinx.coroutines.CoroutineScope import kotlin.math.min /** @@ -168,23 +166,21 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at lineBottom: Float, isImageLine: Boolean ) { - ReadBook.book?.let { book -> - ImageProvider.getImage(book, textPage.chapterIndex, textChar.charData, true) - ?.let { - val rectF = if (isImageLine) { - RectF(textChar.start, lineTop, textChar.end, lineBottom) - } else { - /*以宽度为基准保持图片的原始比例叠加,当div为负数时,允许高度比字符更高*/ - val h = (textChar.end - textChar.start) / it.width * it.height - val div = (lineBottom - lineTop - h) / 2 - RectF(textChar.start, lineTop + div, textChar.end, lineBottom - div) - } - kotlin.runCatching { - canvas.drawBitmap(it, null, rectF, null) - }.onFailure { e -> - context.toastOnUi(e.localizedMessage) - } - } + val book = ReadBook.book ?: return + ImageProvider.getImage(book, textPage.chapterIndex, textChar.charData, true)?.let { + val rectF = if (isImageLine) { + RectF(textChar.start, lineTop, textChar.end, lineBottom) + } else { + /*以宽度为基准保持图片的原始比例叠加,当div为负数时,允许高度比字符更高*/ + val h = (textChar.end - textChar.start) / it.width * it.height + val div = (lineBottom - lineTop - h) / 2 + RectF(textChar.start, lineTop + div, textChar.end, lineBottom - div) + } + kotlin.runCatching { + canvas.drawBitmap(it, null, rectF, null) + }.onFailure { e -> + context.toastOnUi(e.localizedMessage) + } } } @@ -242,9 +238,9 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at } val page = relativePage(relativePos) for ((lineIndex, textLine) in page.textLines.withIndex()) { - if (y > textLine.lineTop + relativeOffset && y < textLine.lineBottom + relativeOffset) { + if (textLine.isTouch(y, relativeOffset)) { for ((charIndex, textChar) in textLine.textChars.withIndex()) { - if (x > textChar.start && x < textChar.end) { + if (textChar.isTouch(x)) { if (textChar.isImage) { activity?.supportFragmentManager?.let { PhotoDialog.show(it, page.chapterIndex, textChar.charData) @@ -278,13 +274,12 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at if (relativeOffset >= ChapterProvider.visibleHeight) return } for ((lineIndex, textLine) in relativePage(relativePos).textLines.withIndex()) { - if (y > textLine.lineTop + relativeOffset && y < textLine.lineBottom + relativeOffset) { + if (textLine.isTouch(y, relativeOffset)) { for ((charIndex, textChar) in textLine.textChars.withIndex()) { - if (x > textChar.start && x < textChar.end) { + if (textChar.isTouch(x)) { if (selectStart[0] != relativePos || selectStart[1] != lineIndex || selectStart[2] != charIndex) { - if (selectToInt(relativePos, lineIndex, charIndex) > selectToInt( - selectEnd - ) + if (selectToInt(relativePos, lineIndex, charIndex) + > selectToInt(selectEnd) ) { return } @@ -320,15 +315,10 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at if (!callBack.isScroll) return if (relativeOffset >= ChapterProvider.visibleHeight) return } - Log.e("y", "$y") for ((lineIndex, textLine) in relativePage(relativePos).textLines.withIndex()) { - if (y > textLine.lineTop + relativeOffset - && y < textLine.lineBottom + relativeOffset - ) { - Log.e("line", "$relativePos $lineIndex") + if (textLine.isTouch(y, relativeOffset)) { for ((charIndex, textChar) in textLine.textChars.withIndex()) { - if (x > textChar.start && x < textChar.end) { - Log.e("char", "$relativePos $lineIndex $charIndex") + if (textChar.isTouch(x)) { if (selectEnd[0] != relativePos || selectEnd[1] != lineIndex || selectEnd[2] != charIndex @@ -540,7 +530,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at fun onCancelSelect() val headerHeight: Int val pageFactory: TextPageFactory - val scope: CoroutineScope val isScroll: Boolean } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChar.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChar.kt index 6a0e9b0f4..abe2e0813 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChar.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChar.kt @@ -6,4 +6,10 @@ data class TextChar( var end: Float, var selected: Boolean = false, var isImage: Boolean = false -) \ No newline at end of file +) { + + fun isTouch(x: Float): Boolean { + return x > start && x < end + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt index 57006ad8f..8ef5dec58 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt @@ -37,4 +37,8 @@ data class TextLine( fun getTextCharsCount(): Int { return textChars.size } + + fun isTouch(y: Float, relativeOffset: Float): Boolean { + return y > lineTop + relativeOffset && y < lineBottom + relativeOffset + } }