Using steps
Glide add
compile('com.github.bumptech.glide:glide:4.6.1') {
exclude group: "com.android.support"
}
// glide kotlin's Toolkit
kapt 'com.github.bumptech.glide:compiler:4.6.1'
compile "com.github.bumptech.glide:okhttp3-integration:4.5.0"
compile 'com.github.bumptech.glide:annotations:4.6.1'
Package image loading class
At present, only a simple package is provided. Of course, you can continue to expand according to the needs of the project
/**
* Created by Zhuge on February 28, 2018
*/
class ImageLoad {
open fun load(context: WeakReference<Context>, url: String?, image: ImageView?) {
if (image == null) return
// The specific image loading implementation can be implemented by using a third-party framework or by itself,
//Here is an example of using glide 4.0:
var requestOptions = RequestOptions().centerCrop()
.placeholder(R.drawable.default_banner)
.error(R.drawable.default_banner)
.transform(CenterCrop())
.format(DecodeFormat.PREFER_RGB_565)
.priority(Priority.LOW)
.dontAnimate()
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
Glide.with(context.get()!!.applicationContext)
.load(url)
.apply(requestOptions)
.into(object : DrawableImageViewTarget(image) {
})
}
open fun load(context: WeakReference<Context>, url: String?, image: ImageView?, transformation: BitmapTransformation) {
if (image == null) return
// Specific picture loading logic
}
open fun load(holder: Int, context: WeakReference<Context>, url: String, image: ImageView?, width: Int, height: Int) {
if (image == null) return
// Specific picture loading logic
}
open fun loadCircle(context: WeakReference<Context>, url: String?, image: ImageView?, width_height: Int) {
if (image == null) return
// Specific picture loading logic
}
open fun loadRound(context: WeakReference<Context>, url: String, image: ImageView?, width: Int, height: Int, round: Int) {
if (image == null) return
// Specific picture loading logic
}
open fun clearCache(context: WeakReference<Context>) {
// Force clear cache, either memory cache or hard disk cache
Glide Use example:
Glide.get(context.get()!!.applicationContext).clearMemory()
System.gc()
}
}
Instructions
// Load round head
ImageLoad().loadCircle(WeakReference(mContext), remark.user_info.portrait, viewHolder.civ_avatar,40)
// Load normal picture
ImageLoad().load(WeakReference(mContext), news.image_1, holder.imageView, width, height)
// Load fillet image
ImageLoad().loadRound(WeakReference(mContext), briefCard["icon"].toString(), holder.image, 5)
When the list loads pictures, it will increase the memory of the application, but Glide provides us with an API to reduce unnecessary memory loss when the list is loaded. Take recyclerview for example:
recyclerview.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
when (newState) {
2 -> { // SCROLL_STATE_FLING
Glide.with(activity.applicationContext).pauseRequests()
}
0 -> { // SCROLL_STATE_IDLE
Glide.with(activity.applicationContext).resumeRequests()
}
1 -> { // SCROLL_STATE_TOUCH_SCROLL
Glide.with(activity.applicationContext).resumeRequests()
}
}
}
})
In the process of list sliding, we can call pauseRequests to pause the loading of pictures, and then call resumeRequests to resume the loading after the sliding. Of course, if you want to reduce the application memory overhead, you can also call ImageLoad().clearCache(WeakReference(this@MainActivity.applicationContext)) to clear the memory cache of Glide