修改图片绘制

pull/1785/head
kunfei 2 years ago
parent 20edfe0fd6
commit 4582ca2a85
  1. 78
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt

@ -83,43 +83,36 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
private fun drawPage(canvas: Canvas) { private fun drawPage(canvas: Canvas) {
var relativeOffset = relativeOffset(0) var relativeOffset = relativeOffset(0)
textPage.textLines.forEach { textLine -> textPage.textLines.forEach { textLine ->
draw(canvas, textLine, relativeOffset) draw(canvas, textPage, textLine, relativeOffset)
} }
if (!callBack.isScroll) return if (!callBack.isScroll) return
//滚动翻页 //滚动翻页
if (!pageFactory.hasNext()) return if (!pageFactory.hasNext()) return
val nextPage = relativePage(1) val textPage1 = relativePage(1)
relativeOffset = relativeOffset(1) relativeOffset = relativeOffset(1)
nextPage.textLines.forEach { textLine -> textPage1.textLines.forEach { textLine ->
draw(canvas, textLine, relativeOffset) draw(canvas, textPage1, textLine, relativeOffset)
} }
if (!pageFactory.hasNextPlus()) return if (!pageFactory.hasNextPlus()) return
relativeOffset = relativeOffset(2) relativeOffset = relativeOffset(2)
if (relativeOffset < ChapterProvider.visibleHeight) { if (relativeOffset < ChapterProvider.visibleHeight) {
relativePage(2).textLines.forEach { textLine -> val textPage2 = relativePage(2)
draw(canvas, textLine, relativeOffset) textPage2.textLines.forEach { textLine ->
draw(canvas, textPage2, textLine, relativeOffset)
} }
} }
} }
private fun draw( private fun draw(
canvas: Canvas, canvas: Canvas,
textPage: TextPage,
textLine: TextLine, textLine: TextLine,
relativeOffset: Float, relativeOffset: Float,
) { ) {
val lineTop = textLine.lineTop + relativeOffset val lineTop = textLine.lineTop + relativeOffset
val lineBase = textLine.lineBase + relativeOffset val lineBase = textLine.lineBase + relativeOffset
val lineBottom = textLine.lineBottom + relativeOffset val lineBottom = textLine.lineBottom + relativeOffset
drawChars( drawChars(canvas, textPage, textLine, lineTop, lineBase, lineBottom)
canvas,
textLine.textChars,
lineTop,
lineBase,
lineBottom,
textLine.isTitle,
textLine.isReadAloud,
textLine.isImage
)
} }
/** /**
@ -127,23 +120,21 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
*/ */
private fun drawChars( private fun drawChars(
canvas: Canvas, canvas: Canvas,
textChars: List<TextChar>, textPage: TextPage,
textLine: TextLine,
lineTop: Float, lineTop: Float,
lineBase: Float, lineBase: Float,
lineBottom: Float, lineBottom: Float,
isTitle: Boolean,
isReadAloud: Boolean,
isImageLine: Boolean
) { ) {
val textPaint = if (isTitle) { val textPaint = if (textLine.isTitle) {
ChapterProvider.titlePaint ChapterProvider.titlePaint
} else { } else {
ChapterProvider.contentPaint ChapterProvider.contentPaint
} }
val textColor = if (isReadAloud) context.accentColor else ReadBookConfig.textColor val textColor = if (textLine.isReadAloud) context.accentColor else ReadBookConfig.textColor
textChars.forEach { textLine.textChars.forEach {
if (it.isImage) { if (it.isImage) {
drawImage(canvas, it, lineTop, lineBottom, isImageLine) drawImage(canvas, textPage, textLine, it, lineTop, lineBottom, textLine.isImage)
} else { } else {
textPaint.color = textColor textPaint.color = textColor
if (it.isSearchResult) { if (it.isSearchResult) {
@ -162,6 +153,8 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
*/ */
private fun drawImage( private fun drawImage(
canvas: Canvas, canvas: Canvas,
textPage: TextPage,
textLine: TextLine,
textChar: TextChar, textChar: TextChar,
lineTop: Float, lineTop: Float,
lineBottom: Float, lineBottom: Float,
@ -177,18 +170,22 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
(lineBottom - lineTop).toInt() (lineBottom - lineTop).toInt()
) )
}.onSuccess { bitmap -> }.onSuccess { bitmap ->
val rectF = if (isImageLine) { relativeOffset(textPage)?.let { relativeOffset ->
RectF(textChar.start, lineTop, textChar.end, lineBottom) val lineTopNow = textLine.lineTop + relativeOffset
} else { val lineBottomNow = textLine.lineBottom + relativeOffset
/*以宽度为基准保持图片的原始比例叠加,当div为负数时,允许高度比字符更高*/ val rectF = if (isImageLine) {
val h = (textChar.end - textChar.start) / bitmap.width * bitmap.height RectF(textChar.start, lineTopNow, textChar.end, lineBottomNow)
val div = (lineBottom - lineTop - h) / 2 } else {
RectF(textChar.start, lineTop + div, textChar.end, lineBottom - div) /*以宽度为基准保持图片的原始比例叠加,当div为负数时,允许高度比字符更高*/
} val h = (textChar.end - textChar.start) / bitmap.width * bitmap.height
kotlin.runCatching { val div = (lineBottomNow - lineTopNow - h) / 2
canvas.drawBitmap(bitmap, null, rectF, null) RectF(textChar.start, lineTopNow + div, textChar.end, lineBottomNow - div)
}.onFailure { e -> }
context.toastOnUi(e.localizedMessage) kotlin.runCatching {
canvas.drawBitmap(bitmap, null, rectF, null)
}.onFailure { e ->
context.toastOnUi(e.localizedMessage)
}
} }
} }
} }
@ -531,6 +528,15 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
} }
private fun relativeOffset(textPage: TextPage): Float? {
return when (textPage) {
this.textPage -> relativeOffset(0)
pageFactory.nextPage -> relativeOffset(1)
pageFactory.nextPlusPage -> relativeOffset(2)
else -> null
}
}
fun relativePage(relativePos: Int): TextPage { fun relativePage(relativePos: Int): TextPage {
return when (relativePos) { return when (relativePos) {
0 -> textPage 0 -> textPage

Loading…
Cancel
Save