pull/47/head
kunfei 5 years ago
parent b08d7b8752
commit 9303f0aab5
  1. 259
      app/src/main/java/io/legado/app/help/ImageLoader.kt
  2. 6
      app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt
  3. 2
      app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt
  4. 2
      app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt
  5. 2
      app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt
  6. 6
      app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt
  7. 2
      app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt
  8. 2
      app/src/main/java/io/legado/app/ui/main/bookshelf/BooksAdapter.kt
  9. 2
      app/src/main/java/io/legado/app/ui/main/rss/RssAdapter.kt
  10. 2
      app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt

@ -4,264 +4,45 @@ import android.content.Context
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.net.Uri import android.net.Uri
import android.widget.ImageView
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder 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 import java.io.File
object ImageLoader { object ImageLoader {
fun load(context: Context, url: String?): ImageLoadBuilder<String> { fun load(context: Context, path: String?): RequestBuilder<Drawable> {
return ImageLoadBuilder(context, url) if (path?.startsWith("http", true) == true) {
} return Glide.with(context).load(path)
}
fun load(context: Context, @DrawableRes resId: Int?): ImageLoadBuilder<Int> { kotlin.runCatching {
return ImageLoadBuilder(context, resId) return Glide.with(context).load(File(path))
} }
return Glide.with(context).load(path)
fun load(context: Context, file: File?): ImageLoadBuilder<File> {
return ImageLoadBuilder(context, file)
}
fun load(context: Context, uri: Uri?): ImageLoadBuilder<Uri> {
return ImageLoadBuilder(context, uri)
}
fun load(context: Context, drawable: Drawable?): ImageLoadBuilder<Drawable> {
return ImageLoadBuilder(context, drawable)
} }
fun load(context: Context, bitmap: Bitmap?): ImageLoadBuilder<Bitmap> { fun load(context: Context, @DrawableRes resId: Int?): RequestBuilder<Drawable> {
return ImageLoadBuilder(context, bitmap) return Glide.with(context).load(resId)
} }
fun load(context: Context, bytes: ByteArray?): ImageLoadBuilder<ByteArray> { fun load(context: Context, file: File?): RequestBuilder<Drawable> {
return ImageLoadBuilder(context, bytes) return Glide.with(context).load(file)
} }
fun load(context: Context, uri: Uri?): RequestBuilder<Drawable> {
fun with(context: Context): ImageLoadBuilder<Any> { return Glide.with(context).load(uri)
return ImageLoadBuilder(context)
} }
fun clear(imageView: ImageView) { fun load(context: Context, drawable: Drawable?): RequestBuilder<Drawable> {
with(imageView.context).clear(imageView) return Glide.with(context).load(drawable)
} }
class ImageLoadBuilder<S> constructor(context: Context, private var source: S? = null) { fun load(context: Context, bitmap: Bitmap?): RequestBuilder<Drawable> {
return Glide.with(context).load(bitmap)
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<S> {
this.source = source
return this
}
fun bitmapTransform(transformation: Transformation<Bitmap>): ImageLoadBuilder<S> {
requestOptions = requestOptions.apply(RequestOptions.bitmapTransform(transformation))
return this
}
fun toRound(corner: Int): ImageLoadBuilder<S> {
requestOptions = requestOptions.transform(RoundedCorners(corner))
return this
}
fun toCropRound(corner: Int): ImageLoadBuilder<S> {
requestOptions = requestOptions.transform(CenterCrop(), RoundedCorners(corner))
return this
}
fun toCircle(): ImageLoadBuilder<S> {
requestOptions = requestOptions.circleCrop()
return this
}
fun centerInside(): ImageLoadBuilder<S> {
requestOptions = requestOptions.centerInside()
return this
}
fun fitCenter(): ImageLoadBuilder<S> {
requestOptions = requestOptions.fitCenter()
return this
}
fun centerCrop(): ImageLoadBuilder<S> {
requestOptions = requestOptions.centerCrop()
return this
}
fun crossFade(): ImageLoadBuilder<S> {
crossFade = true
return this
}
fun noCache(): ImageLoadBuilder<S> {
noCache = true
return this
}
fun placeholder(placeholder: Drawable): ImageLoadBuilder<S> {
requestOptions = requestOptions.placeholder(placeholder)
return this
}
fun placeholder(@DrawableRes resId: Int): ImageLoadBuilder<S> {
requestOptions = requestOptions.placeholder(resId)
return this
}
fun error(drawable: Drawable): ImageLoadBuilder<S> {
requestOptions = requestOptions.error(drawable)
return this
}
fun error(@DrawableRes resId: Int): ImageLoadBuilder<S> {
requestOptions = requestOptions.error(resId)
return this
}
fun override(width: Int, height: Int): ImageLoadBuilder<S> {
requestOptions = requestOptions.override(width, height)
return this
}
fun override(size: Int): ImageLoadBuilder<S> {
requestOptions = requestOptions.override(size)
return this
}
fun clear(imageView: ImageView) {
manager.clear(imageView)
}
fun downloadOnly(target: ImageViewTarget<File>) {
manager.downloadOnly().load(source).into(Target(target))
}
fun setAsDrawable(imageView: ImageView) {
asDrawable().into(imageView)
}
fun setAsDrawable(target: ImageViewTarget<Drawable>) {
asDrawable().into(Target(target))
}
fun setAsBitmap(imageView: ImageView) {
asBitmap().into(imageView)
}
fun setAsBitmap(target: ImageViewTarget<Bitmap>) {
asBitmap().into(Target(target))
}
fun setAsGif(imageView: ImageView) {
asGif().into(imageView)
}
fun setAsGif(target: ImageViewTarget<GifDrawable>) {
asGif().into(Target(target))
}
fun setAsFile(imageView: ImageView) {
asFile().into(imageView)
}
fun setAsFile(target: ImageViewTarget<File>) {
asFile().into(Target(target))
}
private fun asDrawable(): RequestBuilder<Drawable> {
var builder: RequestBuilder<Drawable> = ensureOptions(manager.asDrawable().load(source))
if (crossFade) {
builder = builder.transition(DrawableTransitionOptions.withCrossFade())
}
return builder
}
private fun asBitmap(): RequestBuilder<Bitmap> {
var builder: RequestBuilder<Bitmap> = ensureOptions(manager.asBitmap().load(source))
if (crossFade) {
builder = builder.transition(BitmapTransitionOptions.withCrossFade())
}
return builder
}
private fun asGif(): RequestBuilder<GifDrawable> {
var builder: RequestBuilder<GifDrawable> = ensureOptions(manager.asGif().load(source))
if (crossFade) {
builder = builder.transition(DrawableTransitionOptions.withCrossFade())
}
return builder
}
private fun asFile(): RequestBuilder<File> {
return manager.asFile().load(source)
}
private fun <ResourceType> ensureOptions(builder: RequestBuilder<ResourceType>): RequestBuilder<ResourceType> {
return builder.apply(requestOptions.diskCacheStrategy(if (noCache) DiskCacheStrategy.NONE else DiskCacheStrategy.AUTOMATIC))
}
private inner class Target<R> constructor(private val target: ImageViewTarget<R>) :
com.bumptech.glide.request.target.ImageViewTarget<R>(target.view) {
init {
if (this.target.waitForLayout) {
waitForLayout()
}
}
override fun onResourceReady(resource: R, transition: Transition<in R>?) {
if (!target.onResourceReady(resource)) {
super.onResourceReady(resource, transition)
}
}
override fun setResource(resource: R?) {
target.setResource(resource)
}
}
} }
abstract class ImageViewTarget<R>(val view: ImageView) { fun load(context: Context, bytes: ByteArray?): RequestBuilder<Drawable> {
internal var waitForLayout: Boolean = false return Glide.with(context).load(bytes)
fun waitForLayout(): ImageViewTarget<R> {
waitForLayout = true
return this
}
fun setResource(resource: R?) {
}
fun onResourceReady(resource: R?): Boolean {
return false
}
} }
} }

@ -10,7 +10,6 @@ import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.Bus import io.legado.app.constant.Bus
import io.legado.app.constant.Status import io.legado.app.constant.Status
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BlurTransformation
import io.legado.app.help.ImageLoader import io.legado.app.help.ImageLoader
import io.legado.app.service.help.AudioPlay import io.legado.app.service.help.AudioPlay
import io.legado.app.ui.chapterlist.ChapterListActivity import io.legado.app.ui.chapterlist.ChapterListActivity
@ -85,13 +84,12 @@ class AudioPlayActivity : VMBaseActivity<AudioPlayViewModel>(R.layout.activity_a
.placeholder(R.drawable.image_cover_default) .placeholder(R.drawable.image_cover_default)
.error(R.drawable.image_cover_default) .error(R.drawable.image_cover_default)
.centerCrop() .centerCrop()
.setAsDrawable(iv_cover) .into(iv_cover)
ImageLoader.load(this, path) ImageLoader.load(this, path)
.placeholder(R.drawable.image_cover_default) .placeholder(R.drawable.image_cover_default)
.error(R.drawable.image_cover_default) .error(R.drawable.image_cover_default)
.bitmapTransform(BlurTransformation(this, 25))
.centerCrop() .centerCrop()
.setAsDrawable(iv_bg) .into(iv_bg)
} }
private fun playButton() { private fun playButton() {

@ -89,7 +89,7 @@ class BookInfoActivity : VMBaseActivity<BookInfoViewModel>(R.layout.activity_boo
.placeholder(R.drawable.image_cover_default) .placeholder(R.drawable.image_cover_default)
.error(R.drawable.image_cover_default) .error(R.drawable.image_cover_default)
.centerCrop() .centerCrop()
.setAsDrawable(iv_cover) .into(iv_cover)
} }
val kinds = book.getKindList() val kinds = book.getKindList()
if (kinds.isEmpty()) { if (kinds.isEmpty()) {

@ -153,7 +153,7 @@ class BgTextConfigDialog : DialogFragment() {
with(holder.itemView) { with(holder.itemView) {
ImageLoader.load(context, context.assets.open("bg/$item").readBytes()) ImageLoader.load(context, context.assets.open("bg/$item").readBytes())
.centerCrop() .centerCrop()
.setAsBitmap(iv_bg) .into(iv_bg)
tv_name.text = item.substringBeforeLast(".") tv_name.text = item.substringBeforeLast(".")
this.onClick { this.onClick {
ReadBookConfig.getConfig().setBg(1, item) ReadBookConfig.getConfig().setBg(1, item)

@ -248,7 +248,7 @@ class ReadStyleDialog : DialogFragment() {
} }
ReadBookConfig.getConfig(i).apply { ReadBookConfig.getConfig(i).apply {
when (bgType()) { when (bgType()) {
2 -> ImageLoader.load(requireContext(), bgStr()).centerCrop().setAsDrawable(iv) 2 -> ImageLoader.load(requireContext(), bgStr()).centerCrop().into(iv)
else -> iv.setImageDrawable(bgDrawable(100, 150)) else -> iv.setImageDrawable(bgDrawable(100, 150))
} }
} }

@ -72,7 +72,7 @@ class SearchAdapter(context: Context, val callBack: CallBack) :
.placeholder(R.drawable.image_cover_default) .placeholder(R.drawable.image_cover_default)
.error(R.drawable.image_cover_default) .error(R.drawable.image_cover_default)
.centerCrop() .centerCrop()
.setAsDrawable(iv_cover) .into(iv_cover)
} }
onClick { onClick {
callBack.showBookInfo(searchBook.name, searchBook.author) callBack.showBookInfo(searchBook.name, searchBook.author)
@ -89,7 +89,7 @@ class SearchAdapter(context: Context, val callBack: CallBack) :
.placeholder(R.drawable.image_cover_default) .placeholder(R.drawable.image_cover_default)
.error(R.drawable.image_cover_default) .error(R.drawable.image_cover_default)
.centerCrop() .centerCrop()
.setAsDrawable(iv_cover) .into(iv_cover)
} }
3 -> { 3 -> {
val kinds = searchBook.getKindList() val kinds = searchBook.getKindList()
@ -136,6 +136,8 @@ class SearchAdapter(context: Context, val callBack: CallBack) :
} }
5 -> tv_introduce.text = 5 -> tv_introduce.text =
context.getString(R.string.intro_show, searchBook.intro) context.getString(R.string.intro_show, searchBook.intro)
else -> {
}
} }
} }
} }

@ -63,7 +63,7 @@ class ExploreShowAdapter(context: Context, val callBack: CallBack) :
.placeholder(R.drawable.image_cover_default) .placeholder(R.drawable.image_cover_default)
.error(R.drawable.image_cover_default) .error(R.drawable.image_cover_default)
.centerCrop() .centerCrop()
.setAsDrawable(iv_cover) .into(iv_cover)
} }
onClick { onClick {
callBack.showBookInfo(item.toBook()) callBack.showBookInfo(item.toBook())

@ -30,7 +30,7 @@ class BooksAdapter(context: Context, private val callBack: CallBack) :
.placeholder(R.drawable.image_cover_default) .placeholder(R.drawable.image_cover_default)
.error(R.drawable.image_cover_default) .error(R.drawable.image_cover_default)
.centerCrop() .centerCrop()
.setAsDrawable(iv_cover) .into(iv_cover)
} }
onClick { callBack.open(item) } onClick { callBack.open(item) }
onLongClick { onLongClick {

@ -19,7 +19,7 @@ class RssAdapter(context: Context, val callBack: CallBack) :
.centerCrop() .centerCrop()
.placeholder(R.drawable.image_rss) .placeholder(R.drawable.image_rss)
.error(R.drawable.image_rss) .error(R.drawable.image_rss)
.setAsBitmap(iv_icon) .into(iv_icon)
onClick { callBack.openRss(item) } onClick { callBack.openRss(item) }
} }
} }

@ -28,7 +28,7 @@ class RssArticlesAdapter(context: Context, val callBack: CallBack) :
} else { } else {
image_view.visible() image_view.visible()
ImageLoader.load(context, item.image) ImageLoader.load(context, item.image)
.setAsBitmap(image_view) .into(image_view)
} }
if (item.read) { if (item.read) {
tv_title.textColorResource = R.color.tv_text_summary tv_title.textColorResource = R.color.tv_text_summary

Loading…
Cancel
Save