正文用glide加载有问题

pull/1785/head
kunfei 3 years ago
parent 4647c8ecd2
commit a6e975ee57
  1. 13
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  2. 33
      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,23 +156,20 @@ 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()
) )
}.onSuccess { bitmap ->
relativeOffset(textPage)?.let { relativeOffset -> relativeOffset(textPage)?.let { relativeOffset ->
val lineTopNow = textLine.lineTop + relativeOffset val lineTopNow = textLine.lineTop + relativeOffset
val lineBottomNow = textLine.lineBottom + relativeOffset val lineBottomNow = textLine.lineBottom + relativeOffset
val rectF = if (isImageLine) { val rectF = if (textLine.isImage) {
RectF(textChar.start, lineTopNow, textChar.end, lineBottomNow) RectF(textChar.start, lineTopNow, textChar.end, lineBottomNow)
} else { } else {
/*以宽度为基准保持图片的原始比例叠加,当div为负数时,允许高度比字符更高*/ /*以宽度为基准保持图片的原始比例叠加,当div为负数时,允许高度比字符更高*/
@ -188,7 +184,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
} }
} }
}
/** /**
* 滚动事件 * 滚动事件

@ -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,32 +51,24 @@ 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)
try {
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
ImageLoader.loadBitmap(appCtx, vFile.absolutePath) return try {
.submit(width, height) BitmapUtils.decodeBitmap(vFile.absolutePath, width, height)
.get()
} catch (e: Exception) { } catch (e: Exception) {
Coroutine.async { Coroutine.async {
putDebug("${vFile.absolutePath} 解码失败\n$e", e) putDebug("${vFile.absolutePath} 解码失败\n$e", e)
@ -92,6 +80,5 @@ object ImageProvider {
errorBitmap errorBitmap
} }
} }
}
} }

Loading…
Cancel
Save