优化图片绘制

pull/898/head
gedoor 4 years ago
parent bd25b785ae
commit c6dcd01fc3
  1. 21
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt

@ -119,8 +119,9 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
lineTop, lineTop,
lineBase, lineBase,
lineBottom, lineBottom,
isTitle = textLine.isTitle, textLine.isTitle,
isReadAloud = textLine.isReadAloud textLine.isReadAloud,
textLine.isImage
) )
} }
@ -135,6 +136,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
lineBottom: Float, lineBottom: Float,
isTitle: Boolean, isTitle: Boolean,
isReadAloud: Boolean, isReadAloud: Boolean,
isImageLine: Boolean
) { ) {
val textPaint = if (isTitle) { val textPaint = if (isTitle) {
ChapterProvider.titlePaint ChapterProvider.titlePaint
@ -145,7 +147,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (isReadAloud) context.accentColor else ReadBookConfig.textColor if (isReadAloud) context.accentColor else ReadBookConfig.textColor
textChars.forEach { textChars.forEach {
if (it.isImage) { if (it.isImage) {
drawImage(canvas, it, lineTop, lineBottom) drawImage(canvas, it, lineTop, lineBottom, isImageLine)
} else { } else {
canvas.drawText(it.charData, it.start, lineBase, textPaint) canvas.drawText(it.charData, it.start, lineBase, textPaint)
} }
@ -163,14 +165,19 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
textChar: TextChar, textChar: TextChar,
lineTop: Float, lineTop: Float,
lineBottom: Float, lineBottom: Float,
isImageLine: Boolean
) { ) {
ReadBook.book?.let { book -> ReadBook.book?.let { book ->
ImageProvider.getImage(book, textPage.chapterIndex, textChar.charData, true) ImageProvider.getImage(book, textPage.chapterIndex, textChar.charData, true)
?.let { ?.let {
/*以宽度为基准保持图片的原始比例叠加,当div为负数时,允许高度比字符更高*/ val rectF = if (isImageLine) {
val h = (textChar.end - textChar.start) / it.width * it.height RectF(textChar.start, lineTop, textChar.end, lineBottom)
val div = (lineBottom - lineTop - h) / 2 } else {
val rectF = RectF(textChar.start, lineTop + div, textChar.end, lineBottom - div) /*以宽度为基准保持图片的原始比例叠加,当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)
}
canvas.drawBitmap(it, null, rectF, null) canvas.drawBitmap(it, null, rectF, null)
} }
} }

Loading…
Cancel
Save