正文用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.data.entities.Bookmark
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.model.ReadBook
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
textLine.textChars.forEach {
if (it.isImage) {
drawImage(canvas, textPage, textLine, it, lineTop, lineBottom, textLine.isImage)
drawImage(canvas, textPage, textLine, it, lineTop, lineBottom)
} else {
textPaint.color = textColor
if (it.isSearchResult) {
@ -157,35 +156,31 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
textLine: TextLine,
textChar: TextChar,
lineTop: Float,
lineBottom: Float,
isImageLine: Boolean
lineBottom: Float
) {
val book = ReadBook.book ?: return
Coroutine.async {
ImageProvider.getImage(
book,
textChar.charData,
ReadBook.bookSource,
(textChar.end - textChar.start).toInt(),
(lineBottom - lineTop).toInt()
)
}.onSuccess { bitmap ->
relativeOffset(textPage)?.let { relativeOffset ->
val lineTopNow = textLine.lineTop + relativeOffset
val lineBottomNow = textLine.lineBottom + relativeOffset
val rectF = if (isImageLine) {
RectF(textChar.start, lineTopNow, textChar.end, lineBottomNow)
} else {
/*以宽度为基准保持图片的原始比例叠加,当div为负数时,允许高度比字符更高*/
val h = (textChar.end - textChar.start) / bitmap.width * bitmap.height
val div = (lineBottomNow - lineTopNow - h) / 2
RectF(textChar.start, lineTopNow + div, textChar.end, lineBottomNow - div)
}
kotlin.runCatching {
canvas.drawBitmap(bitmap, null, rectF, null)
}.onFailure { e ->
context.toastOnUi(e.localizedMessage)
}
val bitmap = ImageProvider.getImage(
book,
textChar.charData,
ReadBook.bookSource,
(textChar.end - textChar.start).toInt(),
(lineBottom - lineTop).toInt()
)
relativeOffset(textPage)?.let { relativeOffset ->
val lineTopNow = textLine.lineTop + relativeOffset
val lineBottomNow = textLine.lineBottom + relativeOffset
val rectF = if (textLine.isImage) {
RectF(textChar.start, lineTopNow, textChar.end, lineBottomNow)
} else {
/*以宽度为基准保持图片的原始比例叠加,当div为负数时,允许高度比字符更高*/
val h = (textChar.end - textChar.start) / bitmap.width * bitmap.height
val div = (lineBottomNow - lineTopNow - h) / 2
RectF(textChar.start, lineTopNow + div, textChar.end, lineBottomNow - div)
}
kotlin.runCatching {
canvas.drawBitmap(bitmap, null, rectF, null)
}.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.help.BookHelp
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.utils.BitmapUtils
import io.legado.app.utils.FileUtils
import io.legado.app.utils.isXml
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import splitties.init.appCtx
import java.io.File
import java.io.FileOutputStream
import kotlin.coroutines.resume
object ImageProvider {
@ -55,42 +51,33 @@ object ImageProvider {
bookSource: BookSource?
): Size {
val file = cacheImage(book, src, bookSource)
return suspendCancellableCoroutine { block ->
kotlin.runCatching {
ImageLoader.loadBitmap(appCtx, file.absolutePath).submit()
.getSize { width, height ->
block.resume(Size(width, height))
}
}.onFailure {
block.resume(Size(errorBitmap.width, errorBitmap.height))
}
}
val op = BitmapFactory.Options()
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight;
op.inJustDecodeBounds = true
BitmapFactory.decodeFile(file.absolutePath, op)
return Size(op.outWidth, op.outHeight)
}
suspend fun getImage(
fun getImage(
book: Book,
src: String,
bookSource: BookSource?,
width: Int,
height: Int
): Bitmap {
return withContext(IO) {
val vFile = cacheImage(book, src, bookSource)
try {
@Suppress("BlockingMethodInNonBlockingContext")
ImageLoader.loadBitmap(appCtx, vFile.absolutePath)
.submit(width, height)
.get()
} catch (e: Exception) {
Coroutine.async {
putDebug("${vFile.absolutePath} 解码失败\n$e", e)
if (FileUtils.readText(vFile.absolutePath).isXml()) {
putDebug("${vFile.absolutePath}为xml,自动删除")
vFile.delete()
}
val vFile = BookHelp.getImage(book, src)
@Suppress("BlockingMethodInNonBlockingContext")
return try {
BitmapUtils.decodeBitmap(vFile.absolutePath, width, height)
} catch (e: Exception) {
Coroutine.async {
putDebug("${vFile.absolutePath} 解码失败\n$e", e)
if (FileUtils.readText(vFile.absolutePath).isXml()) {
putDebug("${vFile.absolutePath}为xml,自动删除")
vFile.delete()
}
errorBitmap
}
errorBitmap
}
}

Loading…
Cancel
Save