diff --git a/app/src/main/java/io/legado/app/api/controller/BookController.kt b/app/src/main/java/io/legado/app/api/controller/BookController.kt index d235e878c..78226e9c0 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookController.kt @@ -11,12 +11,12 @@ import io.legado.app.help.BookHelp import io.legado.app.help.ContentProcessor import io.legado.app.help.glide.ImageLoader import io.legado.app.help.storage.AppWebDav +import io.legado.app.model.BookCover import io.legado.app.model.ReadBook import io.legado.app.model.localBook.EpubFile import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.UmdFile import io.legado.app.model.webBook.WebBook -import io.legado.app.ui.widget.image.CoverImageView import io.legado.app.utils.* import kotlinx.coroutines.runBlocking import splitties.init.appCtx @@ -52,7 +52,7 @@ object BookController { return try { returnData.setData(ftBitmap.get()) } catch (e: Exception) { - returnData.setData(CoverImageView.defaultDrawable.toBitmap()) + returnData.setData(BookCover.defaultDrawable.toBitmap()) } } diff --git a/app/src/main/java/io/legado/app/help/ThemeConfig.kt b/app/src/main/java/io/legado/app/help/ThemeConfig.kt index 805a83255..2206f51b5 100644 --- a/app/src/main/java/io/legado/app/help/ThemeConfig.kt +++ b/app/src/main/java/io/legado/app/help/ThemeConfig.kt @@ -11,7 +11,7 @@ import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.constant.Theme import io.legado.app.lib.theme.ThemeStore -import io.legado.app.ui.widget.image.CoverImageView +import io.legado.app.model.BookCover import io.legado.app.utils.* import splitties.init.appCtx import java.io.File @@ -29,7 +29,7 @@ object ThemeConfig { ReadBookConfig.upBg() applyTheme(context) initNightMode() - CoverImageView.upDefaultCover() + BookCover.upDefaultCover() postEvent(EventBus.RECREATE, "") } diff --git a/app/src/main/java/io/legado/app/model/BookCover.kt b/app/src/main/java/io/legado/app/model/BookCover.kt new file mode 100644 index 000000000..a196cc60c --- /dev/null +++ b/app/src/main/java/io/legado/app/model/BookCover.kt @@ -0,0 +1,54 @@ +package io.legado.app.model + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.drawable.Drawable +import com.bumptech.glide.RequestBuilder +import com.bumptech.glide.request.RequestOptions +import io.legado.app.R +import io.legado.app.constant.PreferKey +import io.legado.app.help.AppConfig +import io.legado.app.help.BlurTransformation +import io.legado.app.help.glide.ImageLoader +import io.legado.app.utils.getPrefBoolean +import io.legado.app.utils.getPrefString +import splitties.init.appCtx + +object BookCover { + + var drawBookName = true + private set + var drawBookAuthor = true + private set + lateinit var defaultDrawable: Drawable + private set + + init { + upDefaultCover() + } + + @SuppressLint("UseCompatLoadingForDrawables") + fun upDefaultCover() { + val isNightTheme = AppConfig.isNightTheme + drawBookName = if (isNightTheme) { + appCtx.getPrefBoolean(PreferKey.coverShowNameN, true) + } else { + appCtx.getPrefBoolean(PreferKey.coverShowName, true) + } + drawBookAuthor = if (isNightTheme) { + appCtx.getPrefBoolean(PreferKey.coverShowAuthorN, true) + } else { + appCtx.getPrefBoolean(PreferKey.coverShowAuthor, true) + } + val key = if (isNightTheme) PreferKey.defaultCoverDark else PreferKey.defaultCover + val path = appCtx.getPrefString(key) + defaultDrawable = Drawable.createFromPath(path) + ?: appCtx.resources.getDrawable(R.drawable.image_cover_default, null) + } + + fun getBlurDefaultCover(context: Context): RequestBuilder { + return ImageLoader.load(context, defaultDrawable) + .apply(RequestOptions.bitmapTransform(BlurTransformation(context, 25))) + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt index 1bb2661f2..d8a63116e 100644 --- a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt @@ -1,7 +1,6 @@ package io.legado.app.ui.book.audio import android.app.Activity -import android.graphics.drawable.Drawable import android.icu.text.SimpleDateFormat import android.os.Build import android.os.Bundle @@ -9,7 +8,6 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import androidx.activity.viewModels -import com.bumptech.glide.RequestBuilder import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions import com.bumptech.glide.request.RequestOptions.bitmapTransform import io.legado.app.R @@ -24,12 +22,12 @@ import io.legado.app.help.BlurTransformation import io.legado.app.help.glide.ImageLoader import io.legado.app.lib.dialogs.alert import io.legado.app.model.AudioPlay +import io.legado.app.model.BookCover import io.legado.app.service.AudioPlayService import io.legado.app.ui.book.changesource.ChangeSourceDialog import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.ui.book.toc.TocActivityResult import io.legado.app.ui.login.SourceLoginActivity -import io.legado.app.ui.widget.image.CoverImageView import io.legado.app.ui.widget.seekbar.SeekBarChangeListener import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding @@ -168,21 +166,16 @@ class AudioPlayActivity : private fun upCover(path: String?) { ImageLoader.load(this, path) - .placeholder(CoverImageView.defaultDrawable) - .error(CoverImageView.defaultDrawable) + .placeholder(BookCover.defaultDrawable) + .error(BookCover.defaultDrawable) .into(binding.ivCover) ImageLoader.load(this, path) .transition(DrawableTransitionOptions.withCrossFade(1500)) - .thumbnail(defaultCover()) + .thumbnail(BookCover.getBlurDefaultCover(this)) .apply(bitmapTransform(BlurTransformation(this, 25))) .into(binding.ivBg) } - private fun defaultCover(): RequestBuilder { - return ImageLoader.load(this, CoverImageView.defaultDrawable) - .apply(bitmapTransform(BlurTransformation(this, 25))) - } - private fun playButton() { when (AudioPlay.status) { Status.PLAY -> AudioPlay.pause(this) diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index bab99579a..e64704d54 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -2,7 +2,6 @@ package io.legado.app.ui.book.info import android.annotation.SuppressLint import android.content.Intent -import android.graphics.drawable.Drawable import android.os.Bundle import android.view.Menu import android.view.MenuItem @@ -10,7 +9,6 @@ import android.widget.CheckBox import android.widget.LinearLayout import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.viewModels -import com.bumptech.glide.RequestBuilder import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions import com.bumptech.glide.request.RequestOptions.bitmapTransform import io.legado.app.R @@ -29,6 +27,7 @@ import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.bottomBackground import io.legado.app.lib.theme.getPrimaryTextColor +import io.legado.app.model.BookCover import io.legado.app.ui.about.AppLogDialog import io.legado.app.ui.book.audio.AudioPlayActivity import io.legado.app.ui.book.changecover.ChangeCoverDialog @@ -40,7 +39,6 @@ import io.legado.app.ui.book.search.SearchActivity import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.ui.book.toc.TocActivityResult import io.legado.app.ui.login.SourceLoginActivity -import io.legado.app.ui.widget.image.CoverImageView import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers.IO @@ -210,16 +208,11 @@ class BookInfoActivity : binding.ivCover.load(book.getDisplayCover(), book.name, book.author) ImageLoader.load(this, book.getDisplayCover()) .transition(DrawableTransitionOptions.withCrossFade(1500)) - .thumbnail(defaultCover()) + .thumbnail(BookCover.getBlurDefaultCover(this)) .apply(bitmapTransform(BlurTransformation(this, 25))) .into(binding.bgBook) //模糊、渐变、缩小效果 } - private fun defaultCover(): RequestBuilder { - return ImageLoader.load(this, CoverImageView.defaultDrawable) - .apply(bitmapTransform(BlurTransformation(this, 25))) - } - private fun upLoading(isLoading: Boolean, chapterList: List? = null) { when { isLoading -> { diff --git a/app/src/main/java/io/legado/app/ui/config/CoverConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/CoverConfigFragment.kt index cff5c2cbb..90ccaeaac 100644 --- a/app/src/main/java/io/legado/app/ui/config/CoverConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/CoverConfigFragment.kt @@ -11,7 +11,7 @@ import io.legado.app.base.BasePreferenceFragment import io.legado.app.constant.PreferKey import io.legado.app.lib.dialogs.selector import io.legado.app.lib.theme.ATH -import io.legado.app.ui.widget.image.CoverImageView +import io.legado.app.model.BookCover import io.legado.app.ui.widget.prefs.SwitchPreference import io.legado.app.utils.* @@ -65,16 +65,16 @@ class CoverConfigFragment : BasePreferenceFragment(), PreferKey.coverShowName -> { findPreference(PreferKey.coverShowAuthor) ?.isEnabled = getPrefBoolean(key) - CoverImageView.upDefaultCover() + BookCover.upDefaultCover() } PreferKey.coverShowNameN -> { findPreference(PreferKey.coverShowAuthorN) ?.isEnabled = getPrefBoolean(key) - CoverImageView.upDefaultCover() + BookCover.upDefaultCover() } PreferKey.coverShowAuthor, PreferKey.coverShowAuthorN -> { - CoverImageView.upDefaultCover() + BookCover.upDefaultCover() } } } @@ -89,7 +89,7 @@ class CoverConfigFragment : BasePreferenceFragment(), context?.selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> if (i == 0) { removePref(PreferKey.defaultCover) - CoverImageView.upDefaultCover() + BookCover.upDefaultCover() } else { selectImage.launch(requestCodeCover) } @@ -102,7 +102,7 @@ class CoverConfigFragment : BasePreferenceFragment(), context?.selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> if (i == 0) { removePref(PreferKey.defaultCoverDark) - CoverImageView.upDefaultCover() + BookCover.upDefaultCover() } else { selectImage.launch(requestCodeCoverDark) } @@ -131,7 +131,7 @@ class CoverConfigFragment : BasePreferenceFragment(), file = FileUtils.createFileIfNotExist(file, "covers", name) file.writeBytes(bytes) putPrefString(preferenceKey, file.absolutePath) - CoverImageView.upDefaultCover() + BookCover.upDefaultCover() } } diff --git a/app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt b/app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt index aadc96e8d..aeb1ba9e9 100644 --- a/app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt @@ -1,6 +1,5 @@ package io.legado.app.ui.widget.image -import android.annotation.SuppressLint import android.content.Context import android.graphics.* import android.graphics.drawable.Drawable @@ -10,16 +9,12 @@ 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.constant.PreferKey import io.legado.app.help.AppConfig import io.legado.app.help.glide.ImageLoader import io.legado.app.lib.theme.accentColor -import io.legado.app.utils.getPrefBoolean -import io.legado.app.utils.getPrefString +import io.legado.app.model.BookCover import io.legado.app.utils.textHeight import io.legado.app.utils.toStringArray -import splitties.init.appCtx /** * 封面 @@ -98,7 +93,7 @@ class CoverImageView @JvmOverloads constructor( } private fun drawNameAuthor(canvas: Canvas) { - if (!drawBookName) return + if (!BookCover.drawBookName) return var startX = width * 0.2f var startY = height * 0.2f name?.toStringArray()?.let { name -> @@ -117,7 +112,7 @@ class CoverImageView @JvmOverloads constructor( } } } - if (!drawBookAuthor) return + if (!BookCover.drawBookAuthor) return author?.toStringArray()?.let { author -> startX = width * 0.8f startY = height * 0.7f @@ -175,46 +170,17 @@ class CoverImageView @JvmOverloads constructor( this.name = name this.author = author if (AppConfig.useDefaultCover) { - ImageLoader.load(context, defaultDrawable) + ImageLoader.load(context, BookCover.defaultDrawable) .centerCrop() .into(this) } else { ImageLoader.load(context, path)//Glide自动识别http://,content://和file:// - .placeholder(defaultDrawable) - .error(defaultDrawable) + .placeholder(BookCover.defaultDrawable) + .error(BookCover.defaultDrawable) .listener(glideListener) .centerCrop() .into(this) } } - companion object { - private var drawBookName = true - private var drawBookAuthor = true - lateinit var defaultDrawable: Drawable - - init { - upDefaultCover() - } - - @SuppressLint("UseCompatLoadingForDrawables") - fun upDefaultCover() { - val isNightTheme = AppConfig.isNightTheme - drawBookName = if (isNightTheme) { - appCtx.getPrefBoolean(PreferKey.coverShowNameN, true) - } else { - appCtx.getPrefBoolean(PreferKey.coverShowName, true) - } - drawBookAuthor = if (isNightTheme) { - appCtx.getPrefBoolean(PreferKey.coverShowAuthorN, true) - } else { - appCtx.getPrefBoolean(PreferKey.coverShowAuthor, true) - } - val key = if (isNightTheme) PreferKey.defaultCoverDark else PreferKey.defaultCover - val path = appCtx.getPrefString(key) - defaultDrawable = Drawable.createFromPath(path) - ?: appCtx.resources.getDrawable(R.drawable.image_cover_default, null) - } - - } }