pull/1333/head
gedoor 3 years ago
parent 56fe95e642
commit 155f969ef3
  1. 2
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  2. 57
      app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt

@ -66,7 +66,9 @@ object PreferKey {
const val transparentStatusBar = "transparentStatusBar" const val transparentStatusBar = "transparentStatusBar"
const val immNavigationBar = "immNavigationBar" const val immNavigationBar = "immNavigationBar"
const val defaultCover = "defaultCover" const val defaultCover = "defaultCover"
const val defaultCoverShowName = "defaultCoverShowName"
const val defaultCoverDark = "defaultCoverDark" const val defaultCoverDark = "defaultCoverDark"
const val defaultCoverDarkShowName = "defaultCoverDarkShowName"
const val replaceEnableDefault = "replaceEnableDefault" const val replaceEnableDefault = "replaceEnableDefault"
const val showBrightnessView = "showBrightnessView" const val showBrightnessView = "showBrightnessView"
const val autoClearExpired = "autoClearExpired" const val autoClearExpired = "autoClearExpired"

@ -6,10 +6,15 @@ import android.graphics.Canvas
import android.graphics.Path import android.graphics.Path
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.util.AttributeSet import android.util.AttributeSet
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.glide.ImageLoader import io.legado.app.help.glide.ImageLoader
import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.getPrefString import io.legado.app.utils.getPrefString
import splitties.init.appCtx import splitties.init.appCtx
@ -28,7 +33,7 @@ class CoverImageView @JvmOverloads constructor(
internal var height: Float = 0.toFloat() internal var height: Float = 0.toFloat()
var path: String? = null var path: String? = null
private set private set
private var defaultCover = true
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val measuredWidth = MeasureSpec.getSize(widthMeasureSpec) val measuredWidth = MeasureSpec.getSize(widthMeasureSpec)
@ -70,6 +75,33 @@ class CoverImageView @JvmOverloads constructor(
minimumWidth = width minimumWidth = width
} }
private val glideListener by lazy {
object : RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>?,
isFirstResource: Boolean
): Boolean {
defaultCover = true
return false
}
override fun onResourceReady(
resource: Drawable?,
model: Any?,
target: Target<Drawable>?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
defaultCover = false
return false
}
}
}
fun load(path: String? = null) { fun load(path: String? = null) {
this.path = path this.path = path
if (AppConfig.useDefaultCover) { if (AppConfig.useDefaultCover) {
@ -80,13 +112,14 @@ class CoverImageView @JvmOverloads constructor(
ImageLoader.load(context, path)//Glide自动识别http://,content://和file:// ImageLoader.load(context, path)//Glide自动识别http://,content://和file://
.placeholder(defaultDrawable) .placeholder(defaultDrawable)
.error(defaultDrawable) .error(defaultDrawable)
.listener(glideListener)
.centerCrop() .centerCrop()
.into(this) .into(this)
} }
} }
companion object { companion object {
private var showBookName = false private var showBookName = true
lateinit var defaultDrawable: Drawable lateinit var defaultDrawable: Drawable
init { init {
@ -95,18 +128,18 @@ class CoverImageView @JvmOverloads constructor(
@SuppressLint("UseCompatLoadingForDrawables") @SuppressLint("UseCompatLoadingForDrawables")
fun upDefaultCover() { fun upDefaultCover() {
val preferKey = val isNightTheme = AppConfig.isNightTheme
if (AppConfig.isNightTheme) PreferKey.defaultCoverDark val key = if (isNightTheme) PreferKey.defaultCoverDark else PreferKey.defaultCover
else PreferKey.defaultCover val path = appCtx.getPrefString(key)
val path = appCtx.getPrefString(preferKey) defaultDrawable = Drawable.createFromPath(path)?.let {
var dw = Drawable.createFromPath(path) val showNameKey = if (isNightTheme) PreferKey.defaultCoverDarkShowName
if (dw == null) { else PreferKey.defaultCoverShowName
showBookName = appCtx.getPrefBoolean(showNameKey)
return@let it
} ?: let {
showBookName = true showBookName = true
dw = appCtx.resources.getDrawable(R.drawable.image_cover_default, null) return@let appCtx.resources.getDrawable(R.drawable.image_cover_default, null)
} else {
showBookName = false
} }
defaultDrawable = dw!!
} }
} }

Loading…
Cancel
Save