pull/1164/head
gedoor 3 years ago
parent 5636f713c9
commit a4ec93b469
  1. 2
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 57
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  3. 8
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChar.kt
  4. 4
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.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.replace.edit.ReplaceEditActivity
import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.ui.widget.dialog.TextDialog
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -109,7 +108,6 @@ class ReadBookActivity : ReadBookBaseActivity(),
TextActionMenu(this, this) TextActionMenu(this, this)
} }
override val scope: CoroutineScope get() = lifecycleScope
override val isInitFinish: Boolean get() = viewModel.isInitFinish override val isInitFinish: Boolean get() = viewModel.isInitFinish
override val isScroll: Boolean get() = binding.readView.isScroll override val isScroll: Boolean get() = binding.readView.isScroll
private val mHandler = Handler(Looper.getMainLooper()) private val mHandler = Handler(Looper.getMainLooper())

@ -5,7 +5,6 @@ import android.graphics.Canvas
import android.graphics.Paint import android.graphics.Paint
import android.graphics.RectF import android.graphics.RectF
import android.util.AttributeSet import android.util.AttributeSet
import android.util.Log
import android.view.View import android.view.View
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.PreferKey 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.getCompatColor
import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.CoroutineScope
import kotlin.math.min import kotlin.math.min
/** /**
@ -168,23 +166,21 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
lineBottom: Float, lineBottom: Float,
isImageLine: Boolean isImageLine: Boolean
) { ) {
ReadBook.book?.let { book -> val book = ReadBook.book ?: return
ImageProvider.getImage(book, textPage.chapterIndex, textChar.charData, true) ImageProvider.getImage(book, textPage.chapterIndex, textChar.charData, true)?.let {
?.let { val rectF = if (isImageLine) {
val rectF = if (isImageLine) { RectF(textChar.start, lineTop, textChar.end, lineBottom)
RectF(textChar.start, lineTop, textChar.end, lineBottom) } else {
} else { /*以宽度为基准保持图片的原始比例叠加,当div为负数时,允许高度比字符更高*/
/*以宽度为基准保持图片的原始比例叠加,当div为负数时,允许高度比字符更高*/ val h = (textChar.end - textChar.start) / it.width * it.height
val h = (textChar.end - textChar.start) / it.width * it.height val div = (lineBottom - lineTop - h) / 2
val div = (lineBottom - lineTop - h) / 2 RectF(textChar.start, lineTop + div, textChar.end, lineBottom - div)
RectF(textChar.start, lineTop + div, textChar.end, lineBottom - div) }
} kotlin.runCatching {
kotlin.runCatching { canvas.drawBitmap(it, null, rectF, null)
canvas.drawBitmap(it, null, rectF, null) }.onFailure { e ->
}.onFailure { e -> context.toastOnUi(e.localizedMessage)
context.toastOnUi(e.localizedMessage) }
}
}
} }
} }
@ -242,9 +238,9 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
val page = relativePage(relativePos) val page = relativePage(relativePos)
for ((lineIndex, textLine) in page.textLines.withIndex()) { 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()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
if (x > textChar.start && x < textChar.end) { if (textChar.isTouch(x)) {
if (textChar.isImage) { if (textChar.isImage) {
activity?.supportFragmentManager?.let { activity?.supportFragmentManager?.let {
PhotoDialog.show(it, page.chapterIndex, textChar.charData) 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 if (relativeOffset >= ChapterProvider.visibleHeight) return
} }
for ((lineIndex, textLine) in relativePage(relativePos).textLines.withIndex()) { 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()) { 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 (selectStart[0] != relativePos || selectStart[1] != lineIndex || selectStart[2] != charIndex) {
if (selectToInt(relativePos, lineIndex, charIndex) > selectToInt( if (selectToInt(relativePos, lineIndex, charIndex)
selectEnd > selectToInt(selectEnd)
)
) { ) {
return return
} }
@ -320,15 +315,10 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (!callBack.isScroll) return if (!callBack.isScroll) return
if (relativeOffset >= ChapterProvider.visibleHeight) return if (relativeOffset >= ChapterProvider.visibleHeight) return
} }
Log.e("y", "$y")
for ((lineIndex, textLine) in relativePage(relativePos).textLines.withIndex()) { for ((lineIndex, textLine) in relativePage(relativePos).textLines.withIndex()) {
if (y > textLine.lineTop + relativeOffset if (textLine.isTouch(y, relativeOffset)) {
&& y < textLine.lineBottom + relativeOffset
) {
Log.e("line", "$relativePos $lineIndex")
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
if (x > textChar.start && x < textChar.end) { if (textChar.isTouch(x)) {
Log.e("char", "$relativePos $lineIndex $charIndex")
if (selectEnd[0] != relativePos if (selectEnd[0] != relativePos
|| selectEnd[1] != lineIndex || selectEnd[1] != lineIndex
|| selectEnd[2] != charIndex || selectEnd[2] != charIndex
@ -540,7 +530,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
fun onCancelSelect() fun onCancelSelect()
val headerHeight: Int val headerHeight: Int
val pageFactory: TextPageFactory val pageFactory: TextPageFactory
val scope: CoroutineScope
val isScroll: Boolean val isScroll: Boolean
} }
} }

@ -6,4 +6,10 @@ data class TextChar(
var end: Float, var end: Float,
var selected: Boolean = false, var selected: Boolean = false,
var isImage: Boolean = false var isImage: Boolean = false
) ) {
fun isTouch(x: Float): Boolean {
return x > start && x < end
}
}

@ -37,4 +37,8 @@ data class TextLine(
fun getTextCharsCount(): Int { fun getTextCharsCount(): Int {
return textChars.size return textChars.size
} }
fun isTouch(y: Float, relativeOffset: Float): Boolean {
return y > lineTop + relativeOffset && y < lineBottom + relativeOffset
}
} }

Loading…
Cancel
Save