(1)首先引入Glide,在app目录的build文件中
apply plugin: 'kotlin-kapt'
...
dependencies {
...
api 'jp.wasabeef:glide-transformations:4.3.0'
implementation 'com.github.bumptech.glide:glide:4.11.0'
kapt 'com.github.bumptech.glide:compiler:4.11.0'
}
(2)设置自定义Class类(本文设置为 MyGlideModule),继承AppGlideModule
@com.bumptech.glide.annotation.GlideModule
public class MyGlideModule() : AppGlideModule() {
override fun applyOptions(context: Context, builder: GlideBuilder) {
super.applyOptions(context, builder)
val calculator = MemorySizeCalculator.Builder(context)
.setMemoryCacheScreens(2f)
.build()
val cacheSize = calculator.memoryCacheSize
val poolSize = calculator.bitmapPoolSize
builder.setMemoryCache(LruResourceCache((cacheSize * 1.5).toLong()))
builder.setBitmapPool(LruBitmapPool((poolSize * 1.5).toLong()))
val diskSize = 1024 * 1024 * 1024;
builder.setDiskCache(
InternalCacheDiskCacheFactory(
context,
"glide_cache",
diskSize.toLong()
)
)
builder.setDiskCache(ExternalCacheDiskCacheFactory(context, "glide_cache", diskSize))
builder.setDiskCache(
DiskLruCacheFactory(
"Android/glide/cache/",
"glide_cache",
diskSize.toLong()
)
)
builder.setDefaultRequestOptions(
RequestOptions().format(DecodeFormat.PREFER_ARGB_8888).disallowHardwareConfig()
)
}
override fun isManifestParsingEnabled(): Boolean {
return false
}
}
(3)在AndroidManifest文件中加入(这一步不确定,但是加了没有问题)
<application
...
<meta-data
android:name="me.samlss.mykotlin.glideuse.MyGlideModule"
android:value="GlideModule" />
...
</application>
(4)最后引用Glide
Glide.with(activity).load(url).into(imageView)
|