Android自定义AppGlideModule,DataFetcher ,ModelLoaderFactory,ModelLoader,Kotlin(1)
假设实现一个简单的功能,对传入要加载的path路径增加一定的筛选、容错或"重定向",需要自定义一个模型,基于这个模型,让Glide自动匹配模型展开加载。
plugins {
id 'org.jetbrains.kotlin.kapt'
}
implementation 'com.github.bumptech.glide:glide:4.16.0'
kapt 'com.github.bumptech.glide:compiler:4.16.0'
Kotlin
import android.content.Context
import android.util.Log
import com.bumptech.glide.Glide
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.Registry
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.AppGlideModule
import java.io.InputStream
@GlideModule
class MyGlideModule : AppGlideModule() {
override fun applyOptions(context: Context, builder: GlideBuilder) {
super.applyOptions(context, builder)
builder.setLogLevel(Log.DEBUG)
}
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
super.registerComponents(context, glide, registry)
registry.append(
VideoCover::class.java,
InputStream::class.java,
VideoCoverLoaderFactory()
)
}
}
Kotlin
class VideoCover {
var path: String? = null
constructor(path: String) {
this.path = path
}
}
Kotlin
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.Bitmap.CompressFormat
import android.graphics.BitmapFactory
import android.util.Log
import com.bumptech.glide.Priority
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.data.DataFetcher
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.InputStream
class VideoCoverFetcher : DataFetcher<InputStream> {
val TAG = "Glide/VideoCoverFetcher"
private var model: VideoCover? = null
private val resId = android.R.drawable.stat_notify_error
constructor(model: VideoCover) {
this.model = model
}
override fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in InputStream>) {
val bmp = BitmapFactory.decodeResource(Resources.getSystem(), resId)
Log.d(TAG, "loadData ${bmp.byteCount}")
callback.onDataReady(ByteArrayInputStream(bitmapToByteArray(bmp)))
}
override fun cleanup() {
Log.d(TAG, "cleanup")
}
override fun cancel() {
Log.d(TAG, "cancel")
}
override fun getDataClass(): Class<InputStream> {
return InputStream::class.java
}
override fun getDataSource(): DataSource {
return DataSource.LOCAL
}
private fun bitmapToByteArray(bitmap: Bitmap): ByteArray {
val bos = ByteArrayOutputStream()
bitmap.compress(CompressFormat.PNG, 0, bos)
return bos.toByteArray()
}
}
Kotlin
import android.util.Log
import com.bumptech.glide.load.model.ModelLoader
import com.bumptech.glide.load.model.ModelLoaderFactory
import com.bumptech.glide.load.model.MultiModelLoaderFactory
import java.io.InputStream
class VideoCoverLoaderFactory : ModelLoaderFactory<VideoCover, InputStream> {
val TAG = "Glide/VideoCoverLoaderFactory"
override fun build(multiFactory: MultiModelLoaderFactory): ModelLoader<VideoCover, InputStream> {
return VideoCoverModuleLoader()
}
override fun teardown() {
Log.d(TAG, "teardown")
}
}
Kotlin
import android.util.Log
import com.bumptech.glide.load.Options
import com.bumptech.glide.load.model.ModelLoader
import com.bumptech.glide.load.model.ModelLoader.LoadData
import com.bumptech.glide.signature.ObjectKey
import java.io.InputStream
class VideoCoverModuleLoader : ModelLoader<VideoCover, InputStream> {
val TAG = "Glide/VideoCoverModuleLoader"
override fun buildLoadData(
model: VideoCover,
width: Int,
height: Int,
options: Options
): ModelLoader.LoadData<InputStream>? {
Log.d(TAG, "buildLoadData")
return LoadData(
VideoCoverSignature(model.path!!), //简单时候可以考虑ObjectKey(model.path!!)
VideoCoverFetcher(model)
)
}
override fun handles(model: VideoCover): Boolean {
return true
}
}
Kotlin
import com.bumptech.glide.load.Key
import java.security.MessageDigest
class VideoCoverSignature() : Key {
private var path: String? = null
constructor(path: String) : this() {
this.path = path
}
override fun updateDiskCacheKey(messageDigest: MessageDigest) {
val ba: ByteArray = path?.toByteArray()!!
messageDigest.update(ba, 0, ba.size)
}
}
Kotlin
import android.graphics.drawable.Drawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.signature.ObjectKey
class MainActivity : AppCompatActivity() {
val TAG = "Glide/MainActivity"
private var image: ImageView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val path = "xxx"
image = findViewById<ImageView>(R.id.image)
GlideApp.with(this)
.load(VideoCover(path))
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
//.signature(ObjectKey(path))
.addListener(object : RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>,
isFirstResource: Boolean
): Boolean {
Log.d(TAG, "onLoadFailed")
return false
}
override fun onResourceReady(
resource: Drawable,
model: Any,
target: Target<Drawable>?,
dataSource: DataSource,
isFirstResource: Boolean
): Boolean {
Log.d(TAG, "onResourceReady")
return false
}
})
.override(500)
.into(image!!)
}
}