正文用glide加载有问题

pull/1785/head
kunfei 3 years ago
parent 4647c8ecd2
commit a6e975ee57
  1. 53
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  2. 49
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt

@ -10,7 +10,6 @@ import io.legado.app.R
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.data.entities.Bookmark import io.legado.app.data.entities.Bookmark
import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.config.ReadBookConfig
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.accentColor
import io.legado.app.model.ReadBook import io.legado.app.model.ReadBook
import io.legado.app.ui.book.read.PhotoDialog import io.legado.app.ui.book.read.PhotoDialog
@ -134,7 +133,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
val textColor = if (textLine.isReadAloud) context.accentColor else ReadBookConfig.textColor val textColor = if (textLine.isReadAloud) context.accentColor else ReadBookConfig.textColor
textLine.textChars.forEach { textLine.textChars.forEach {
if (it.isImage) { if (it.isImage) {
drawImage(canvas, textPage, textLine, it, lineTop, lineBottom, textLine.isImage) drawImage(canvas, textPage, textLine, it, lineTop, lineBottom)
} else { } else {
textPaint.color = textColor textPaint.color = textColor
if (it.isSearchResult) { if (it.isSearchResult) {
@ -157,35 +156,31 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
textLine: TextLine, textLine: TextLine,
textChar: TextChar, textChar: TextChar,
lineTop: Float, lineTop: Float,
lineBottom: Float, lineBottom: Float
isImageLine: Boolean
) { ) {
val book = ReadBook.book ?: return val book = ReadBook.book ?: return
Coroutine.async { val bitmap = ImageProvider.getImage(
ImageProvider.getImage( book,
book, textChar.charData,
textChar.charData, ReadBook.bookSource,
ReadBook.bookSource, (textChar.end - textChar.start).toInt(),
(textChar.end - textChar.start).toInt(), (lineBottom - lineTop).toInt()
(lineBottom - lineTop).toInt() )
) relativeOffset(textPage)?.let { relativeOffset ->
}.onSuccess { bitmap -> val lineTopNow = textLine.lineTop + relativeOffset
relativeOffset(textPage)?.let { relativeOffset -> val lineBottomNow = textLine.lineBottom + relativeOffset
val lineTopNow = textLine.lineTop + relativeOffset val rectF = if (textLine.isImage) {
val lineBottomNow = textLine.lineBottom + relativeOffset RectF(textChar.start, lineTopNow, textChar.end, lineBottomNow)
val rectF = if (isImageLine) { } else {
RectF(textChar.start, lineTopNow, textChar.end, lineBottomNow) /*以宽度为基准保持图片的原始比例叠加,当div为负数时,允许高度比字符更高*/
} else { val h = (textChar.end - textChar.start) / bitmap.width * bitmap.height
/*以宽度为基准保持图片的原始比例叠加,当div为负数时,允许高度比字符更高*/ val div = (lineBottomNow - lineTopNow - h) / 2
val h = (textChar.end - textChar.start) / bitmap.width * bitmap.height RectF(textChar.start, lineTopNow + div, textChar.end, lineBottomNow - div)
val div = (lineBottomNow - lineTopNow - h) / 2 }
RectF(textChar.start, lineTopNow + div, textChar.end, lineBottomNow - div) kotlin.runCatching {
} canvas.drawBitmap(bitmap, null, rectF, null)
kotlin.runCatching { }.onFailure { e ->
canvas.drawBitmap(bitmap, null, rectF, null) context.toastOnUi(e.localizedMessage)
}.onFailure { e ->
context.toastOnUi(e.localizedMessage)
}
} }
} }
} }

@ -9,17 +9,13 @@ import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.help.glide.ImageLoader
import io.legado.app.model.localBook.EpubFile import io.legado.app.model.localBook.EpubFile
import io.legado.app.utils.BitmapUtils
import io.legado.app.utils.FileUtils import io.legado.app.utils.FileUtils
import io.legado.app.utils.isXml import io.legado.app.utils.isXml
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import kotlin.coroutines.resume
object ImageProvider { object ImageProvider {
@ -55,42 +51,33 @@ object ImageProvider {
bookSource: BookSource? bookSource: BookSource?
): Size { ): Size {
val file = cacheImage(book, src, bookSource) val file = cacheImage(book, src, bookSource)
return suspendCancellableCoroutine { block -> val op = BitmapFactory.Options()
kotlin.runCatching { // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight;
ImageLoader.loadBitmap(appCtx, file.absolutePath).submit() op.inJustDecodeBounds = true
.getSize { width, height -> BitmapFactory.decodeFile(file.absolutePath, op)
block.resume(Size(width, height)) return Size(op.outWidth, op.outHeight)
}
}.onFailure {
block.resume(Size(errorBitmap.width, errorBitmap.height))
}
}
} }
suspend fun getImage( fun getImage(
book: Book, book: Book,
src: String, src: String,
bookSource: BookSource?, bookSource: BookSource?,
width: Int, width: Int,
height: Int height: Int
): Bitmap { ): Bitmap {
return withContext(IO) { val vFile = BookHelp.getImage(book, src)
val vFile = cacheImage(book, src, bookSource) @Suppress("BlockingMethodInNonBlockingContext")
try { return try {
@Suppress("BlockingMethodInNonBlockingContext") BitmapUtils.decodeBitmap(vFile.absolutePath, width, height)
ImageLoader.loadBitmap(appCtx, vFile.absolutePath) } catch (e: Exception) {
.submit(width, height) Coroutine.async {
.get() putDebug("${vFile.absolutePath} 解码失败\n$e", e)
} catch (e: Exception) { if (FileUtils.readText(vFile.absolutePath).isXml()) {
Coroutine.async { putDebug("${vFile.absolutePath}为xml,自动删除")
putDebug("${vFile.absolutePath} 解码失败\n$e", e) vFile.delete()
if (FileUtils.readText(vFile.absolutePath).isXml()) {
putDebug("${vFile.absolutePath}为xml,自动删除")
vFile.delete()
}
} }
errorBitmap
} }
errorBitmap
} }
} }

Loading…
Cancel
Save