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 58f6d73fb..5ba2d0958 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 @@ -171,18 +171,19 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at book, textChar.charData, ReadBook.bookSource, - true - )?.let { + (textChar.end - textChar.start).toInt(), + (lineBottom - lineTop).toInt() + )?.let { bitmap -> val rectF = if (isImageLine) { RectF(textChar.start, lineTop, textChar.end, lineBottom) } else { /*以宽度为基准保持图片的原始比例叠加,当div为负数时,允许高度比字符更高*/ - val h = (textChar.end - textChar.start) / it.width * it.height + val h = (textChar.end - textChar.start) / bitmap.width * bitmap.height val div = (lineBottom - lineTop - h) / 2 RectF(textChar.start, lineTop + div, textChar.end, lineBottom - div) } kotlin.runCatching { - canvas.drawBitmap(it, null, rectF, null) + canvas.drawBitmap(bitmap, null, rectF, null) }.onFailure { e -> context.toastOnUi(e.localizedMessage) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt index c3e50c396..877e190be 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt @@ -66,41 +66,54 @@ object ImageProvider { } } - suspend fun getImage( + fun getImage( book: Book, src: String, bookSource: BookSource?, - onUi: Boolean = false, + width: Int, + height: Int ): Bitmap? { - val vFile = BookHelp.getImage(book, src) - if (!vFile.exists()) { - if (book.isEpub()) { - EpubFile.getImage(book, src)?.use { input -> - val newFile = FileUtils.createFileIfNotExist(vFile.absolutePath) - FileOutputStream(newFile).use { output -> - input.copyTo(output) - } - } - } else if (!onUi) { - runBlocking { - BookHelp.saveImage(bookSource, book, src) + val vFile = runBlocking { + cacheImage(book, src, bookSource) + } + return try { + ImageLoader.loadBitmap(appCtx, vFile.absolutePath) + .submit(width, height) + .get() + } catch (e: Exception) { + Coroutine.async { + putDebug("${vFile.absolutePath} 解码失败", e) + if (FileUtils.readText(vFile.absolutePath).isXml()) { + putDebug("${vFile.absolutePath}为xml,自动删除") + vFile.delete() } } + errorBitmap } - return try { + } + + fun getImage( + book: Book, + src: String, + bookSource: BookSource? + ): Bitmap? { + val vFile = runBlocking { + cacheImage(book, src, bookSource) + } + return try { ImageLoader.loadBitmap(appCtx, vFile.absolutePath) - .submit(ChapterProvider.visibleWidth,ChapterProvider.visibleHeight) + .submit(ChapterProvider.visibleWidth, ChapterProvider.visibleHeight) .get() - } catch (e: Exception) { - Coroutine.async { - putDebug("${vFile.absolutePath} 解码失败", e) - if (FileUtils.readText(vFile.absolutePath).isXml()) { - putDebug("${vFile.absolutePath}为xml,自动删除") - vFile.delete() - } - } - errorBitmap - } + } catch (e: Exception) { + Coroutine.async { + putDebug("${vFile.absolutePath} 解码失败", e) + if (FileUtils.readText(vFile.absolutePath).isXml()) { + putDebug("${vFile.absolutePath}为xml,自动删除") + vFile.delete() + } + } + errorBitmap + } } }