This commit is contained in:
Horis 2024-11-21 22:34:33 +08:00
parent 080416a9b3
commit db16345397

View File

@ -1,5 +1,7 @@
package io.legado.app.api.controller package io.legado.app.api.controller
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import androidx.core.graphics.drawable.toBitmap import androidx.core.graphics.drawable.toBitmap
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import io.legado.app.api.ReturnData import io.legado.app.api.ReturnData
@ -29,6 +31,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.File
import java.util.WeakHashMap
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
object BookController { object BookController {
@ -36,6 +39,7 @@ object BookController {
private lateinit var book: Book private lateinit var book: Book
private var bookSource: BookSource? = null private var bookSource: BookSource? = null
private var bookUrl: String = "" private var bookUrl: String = ""
private val defaultCoverCache by lazy { WeakHashMap<Drawable, Bitmap>() }
/** /**
* 书架所有书籍 * 书架所有书籍
@ -77,14 +81,20 @@ object BookController {
} catch (e: Exception) { } catch (e: Exception) {
LogUtils.d("BookController", "Error getCover($startAt) $coverPath\n$e") LogUtils.d("BookController", "Error getCover($startAt) $coverPath\n$e")
try { try {
val defaultBitmap = Glide.with(appCtx) val cached = defaultCoverCache[BookCover.defaultDrawable]
.asBitmap() if (cached == null) {
.load(BookCover.defaultDrawable.toBitmap()) val defaultBitmap = Glide.with(appCtx)
.override(84, 112) .asBitmap()
.centerCrop() .load(BookCover.defaultDrawable.toBitmap())
.submit() .override(84, 112)
.get(3, TimeUnit.SECONDS) .centerCrop()
returnData.setData(defaultBitmap) .submit()
.get(3, TimeUnit.SECONDS)
defaultCoverCache[BookCover.defaultDrawable] = defaultBitmap
returnData.setData(defaultBitmap)
} else {
returnData.setData(cached)
}
} catch (e: Exception) { } catch (e: Exception) {
returnData.setErrorMsg(e.localizedMessage ?: "getCover error") returnData.setErrorMsg(e.localizedMessage ?: "getCover error")
} }