diff --git a/app/src/main/java/io/legado/app/help/ImageLoader.kt b/app/src/main/java/io/legado/app/help/ImageLoader.kt index 836cb6e1d..26a9063d6 100644 --- a/app/src/main/java/io/legado/app/help/ImageLoader.kt +++ b/app/src/main/java/io/legado/app/help/ImageLoader.kt @@ -4,264 +4,45 @@ import android.content.Context import android.graphics.Bitmap import android.graphics.drawable.Drawable import android.net.Uri -import android.widget.ImageView import androidx.annotation.DrawableRes import com.bumptech.glide.Glide import com.bumptech.glide.RequestBuilder -import com.bumptech.glide.RequestManager -import com.bumptech.glide.load.Transformation -import com.bumptech.glide.load.engine.DiskCacheStrategy -import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions -import com.bumptech.glide.load.resource.bitmap.CenterCrop -import com.bumptech.glide.load.resource.bitmap.RoundedCorners -import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions -import com.bumptech.glide.load.resource.gif.GifDrawable -import com.bumptech.glide.request.RequestOptions -import com.bumptech.glide.request.transition.Transition import java.io.File object ImageLoader { - fun load(context: Context, url: String?): ImageLoadBuilder { - return ImageLoadBuilder(context, url) - } - - fun load(context: Context, @DrawableRes resId: Int?): ImageLoadBuilder { - return ImageLoadBuilder(context, resId) - } - - fun load(context: Context, file: File?): ImageLoadBuilder { - return ImageLoadBuilder(context, file) - } - - fun load(context: Context, uri: Uri?): ImageLoadBuilder { - return ImageLoadBuilder(context, uri) - } - - fun load(context: Context, drawable: Drawable?): ImageLoadBuilder { - return ImageLoadBuilder(context, drawable) + fun load(context: Context, path: String?): RequestBuilder { + if (path?.startsWith("http", true) == true) { + return Glide.with(context).load(path) + } + kotlin.runCatching { + return Glide.with(context).load(File(path)) + } + return Glide.with(context).load(path) } - fun load(context: Context, bitmap: Bitmap?): ImageLoadBuilder { - return ImageLoadBuilder(context, bitmap) + fun load(context: Context, @DrawableRes resId: Int?): RequestBuilder { + return Glide.with(context).load(resId) } - fun load(context: Context, bytes: ByteArray?): ImageLoadBuilder { - return ImageLoadBuilder(context, bytes) + fun load(context: Context, file: File?): RequestBuilder { + return Glide.with(context).load(file) } - - fun with(context: Context): ImageLoadBuilder { - return ImageLoadBuilder(context) + fun load(context: Context, uri: Uri?): RequestBuilder { + return Glide.with(context).load(uri) } - fun clear(imageView: ImageView) { - with(imageView.context).clear(imageView) + fun load(context: Context, drawable: Drawable?): RequestBuilder { + return Glide.with(context).load(drawable) } - class ImageLoadBuilder constructor(context: Context, private var source: S? = null) { - - private val manager: RequestManager = Glide.with(context) - private var requestOptions: RequestOptions = RequestOptions() - private var crossFade: Boolean = false - private var noCache: Boolean = false - - fun load(source: S): ImageLoadBuilder { - this.source = source - return this - } - - fun bitmapTransform(transformation: Transformation): ImageLoadBuilder { - requestOptions = requestOptions.apply(RequestOptions.bitmapTransform(transformation)) - return this - } - - fun toRound(corner: Int): ImageLoadBuilder { - requestOptions = requestOptions.transform(RoundedCorners(corner)) - return this - } - - fun toCropRound(corner: Int): ImageLoadBuilder { - requestOptions = requestOptions.transform(CenterCrop(), RoundedCorners(corner)) - return this - } - - fun toCircle(): ImageLoadBuilder { - requestOptions = requestOptions.circleCrop() - return this - } - - fun centerInside(): ImageLoadBuilder { - requestOptions = requestOptions.centerInside() - return this - } - - fun fitCenter(): ImageLoadBuilder { - requestOptions = requestOptions.fitCenter() - return this - } - - fun centerCrop(): ImageLoadBuilder { - requestOptions = requestOptions.centerCrop() - return this - } - - fun crossFade(): ImageLoadBuilder { - crossFade = true - return this - } - - fun noCache(): ImageLoadBuilder { - noCache = true - return this - } - - fun placeholder(placeholder: Drawable): ImageLoadBuilder { - requestOptions = requestOptions.placeholder(placeholder) - return this - } - - fun placeholder(@DrawableRes resId: Int): ImageLoadBuilder { - requestOptions = requestOptions.placeholder(resId) - return this - } - - fun error(drawable: Drawable): ImageLoadBuilder { - requestOptions = requestOptions.error(drawable) - return this - } - - fun error(@DrawableRes resId: Int): ImageLoadBuilder { - requestOptions = requestOptions.error(resId) - return this - } - - fun override(width: Int, height: Int): ImageLoadBuilder { - requestOptions = requestOptions.override(width, height) - return this - } - - fun override(size: Int): ImageLoadBuilder { - requestOptions = requestOptions.override(size) - return this - } - - fun clear(imageView: ImageView) { - manager.clear(imageView) - } - - fun downloadOnly(target: ImageViewTarget) { - manager.downloadOnly().load(source).into(Target(target)) - } - - fun setAsDrawable(imageView: ImageView) { - asDrawable().into(imageView) - } - - fun setAsDrawable(target: ImageViewTarget) { - asDrawable().into(Target(target)) - } - - fun setAsBitmap(imageView: ImageView) { - asBitmap().into(imageView) - } - - fun setAsBitmap(target: ImageViewTarget) { - asBitmap().into(Target(target)) - } - - fun setAsGif(imageView: ImageView) { - asGif().into(imageView) - } - - fun setAsGif(target: ImageViewTarget) { - asGif().into(Target(target)) - } - - fun setAsFile(imageView: ImageView) { - asFile().into(imageView) - } - - fun setAsFile(target: ImageViewTarget) { - asFile().into(Target(target)) - } - - private fun asDrawable(): RequestBuilder { - var builder: RequestBuilder = ensureOptions(manager.asDrawable().load(source)) - - if (crossFade) { - builder = builder.transition(DrawableTransitionOptions.withCrossFade()) - } - - return builder - } - - private fun asBitmap(): RequestBuilder { - var builder: RequestBuilder = ensureOptions(manager.asBitmap().load(source)) - - if (crossFade) { - builder = builder.transition(BitmapTransitionOptions.withCrossFade()) - } - - return builder - } - - private fun asGif(): RequestBuilder { - var builder: RequestBuilder = ensureOptions(manager.asGif().load(source)) - - if (crossFade) { - builder = builder.transition(DrawableTransitionOptions.withCrossFade()) - } - - return builder - } - - private fun asFile(): RequestBuilder { - return manager.asFile().load(source) - } - - private fun ensureOptions(builder: RequestBuilder): RequestBuilder { - return builder.apply(requestOptions.diskCacheStrategy(if (noCache) DiskCacheStrategy.NONE else DiskCacheStrategy.AUTOMATIC)) - } - - private inner class Target constructor(private val target: ImageViewTarget) : - com.bumptech.glide.request.target.ImageViewTarget(target.view) { - - init { - if (this.target.waitForLayout) { - waitForLayout() - } - } - - override fun onResourceReady(resource: R, transition: Transition?) { - if (!target.onResourceReady(resource)) { - super.onResourceReady(resource, transition) - } - } - - override fun setResource(resource: R?) { - target.setResource(resource) - } - } + fun load(context: Context, bitmap: Bitmap?): RequestBuilder { + return Glide.with(context).load(bitmap) } - abstract class ImageViewTarget(val view: ImageView) { - internal var waitForLayout: Boolean = false - - fun waitForLayout(): ImageViewTarget { - waitForLayout = true - return this - } - - fun setResource(resource: R?) { - - } - - fun onResourceReady(resource: R?): Boolean { - return false - } - + fun load(context: Context, bytes: ByteArray?): RequestBuilder { + return Glide.with(context).load(bytes) } - } diff --git a/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt b/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt index 91ec8bd10..cec370757 100644 --- a/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt +++ b/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt @@ -10,7 +10,6 @@ import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Bus import io.legado.app.constant.Status import io.legado.app.data.entities.BookChapter -import io.legado.app.help.BlurTransformation import io.legado.app.help.ImageLoader import io.legado.app.service.help.AudioPlay import io.legado.app.ui.chapterlist.ChapterListActivity @@ -85,13 +84,12 @@ class AudioPlayActivity : VMBaseActivity(R.layout.activity_a .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) .centerCrop() - .setAsDrawable(iv_cover) + .into(iv_cover) ImageLoader.load(this, path) .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) - .bitmapTransform(BlurTransformation(this, 25)) .centerCrop() - .setAsDrawable(iv_bg) + .into(iv_bg) } private fun playButton() { 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 53890274b..d6258f319 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 @@ -89,7 +89,7 @@ class BookInfoActivity : VMBaseActivity(R.layout.activity_boo .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) .centerCrop() - .setAsDrawable(iv_cover) + .into(iv_cover) } val kinds = book.getKindList() if (kinds.isEmpty()) { diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index d8112b538..194e89daf 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -153,7 +153,7 @@ class BgTextConfigDialog : DialogFragment() { with(holder.itemView) { ImageLoader.load(context, context.assets.open("bg/$item").readBytes()) .centerCrop() - .setAsBitmap(iv_bg) + .into(iv_bg) tv_name.text = item.substringBeforeLast(".") this.onClick { ReadBookConfig.getConfig().setBg(1, item) diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt index d64fc4db9..f88a7a0e7 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt @@ -248,7 +248,7 @@ class ReadStyleDialog : DialogFragment() { } ReadBookConfig.getConfig(i).apply { when (bgType()) { - 2 -> ImageLoader.load(requireContext(), bgStr()).centerCrop().setAsDrawable(iv) + 2 -> ImageLoader.load(requireContext(), bgStr()).centerCrop().into(iv) else -> iv.setImageDrawable(bgDrawable(100, 150)) } } diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt index 7339ba392..04d2e46f8 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt @@ -72,7 +72,7 @@ class SearchAdapter(context: Context, val callBack: CallBack) : .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) .centerCrop() - .setAsDrawable(iv_cover) + .into(iv_cover) } onClick { callBack.showBookInfo(searchBook.name, searchBook.author) @@ -89,7 +89,7 @@ class SearchAdapter(context: Context, val callBack: CallBack) : .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) .centerCrop() - .setAsDrawable(iv_cover) + .into(iv_cover) } 3 -> { val kinds = searchBook.getKindList() @@ -136,6 +136,8 @@ class SearchAdapter(context: Context, val callBack: CallBack) : } 5 -> tv_introduce.text = context.getString(R.string.intro_show, searchBook.intro) + else -> { + } } } } diff --git a/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt b/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt index 5daa76472..d25e7f255 100644 --- a/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt @@ -63,7 +63,7 @@ class ExploreShowAdapter(context: Context, val callBack: CallBack) : .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) .centerCrop() - .setAsDrawable(iv_cover) + .into(iv_cover) } onClick { callBack.showBookInfo(item.toBook()) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksAdapter.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksAdapter.kt index 25de68e9a..75b0f7787 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BooksAdapter.kt @@ -30,7 +30,7 @@ class BooksAdapter(context: Context, private val callBack: CallBack) : .placeholder(R.drawable.image_cover_default) .error(R.drawable.image_cover_default) .centerCrop() - .setAsDrawable(iv_cover) + .into(iv_cover) } onClick { callBack.open(item) } onLongClick { diff --git a/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt b/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt index b490c8ec8..63bec82e2 100644 --- a/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt @@ -19,7 +19,7 @@ class RssAdapter(context: Context, val callBack: CallBack) : .centerCrop() .placeholder(R.drawable.image_rss) .error(R.drawable.image_rss) - .setAsBitmap(iv_icon) + .into(iv_icon) onClick { callBack.openRss(item) } } } diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt index 03546d957..4105e1958 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt @@ -28,7 +28,7 @@ class RssArticlesAdapter(context: Context, val callBack: CallBack) : } else { image_view.visible() ImageLoader.load(context, item.image) - .setAsBitmap(image_view) + .into(image_view) } if (item.read) { tv_title.textColorResource = R.color.tv_text_summary