Based on the existing project has a large number of hd beautiful image display modules, so in the use and comparison of Glide and Fresco loading effect and use experience, the two frameworks are very good, but their focus is slightly different, I choose Glide because I like the API style of Glide. It’s easy and doesn’t involve custom views.
Glide address: bumptech. Making. IO/Glide
This focus will be on the application of memory management, of course, for the image processing is also a very important part of memory management.
Start with the renderings
Using the step
Glide add
compile('com. Making. Bumptech. Glide: glide: 4.6.1') {
exclude group: "com.android.support"} // Glide kotlin toolkit kapt'com. Making. Bumptech. Glide: the compiler: 4.6.1'
compile "Com. Making. Bumptech. Glide: okhttp3 - integration: 4.5.0." "
compile 'com. Making. Bumptech. Glide: annotations: 4.6.1'
Copy the code
Encapsulates the image loading class
It only provides a simple package, but you can expand it as your project needs it
/** * Created by moment on 2018/2/6. */ class ImageLoad { open fun load(context: WeakReference<Context>, url: String? , image: ImageView?) {if (image == null) return// You can use a third-party framework to load the image, or you can implement it yourself. Here is an example of how to use Glide4.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} Open fun load(holder: Int, context: WeakReference< context >, URL: String, image: ImageView? , width: Int, height: Int) {if (image == null) return} Open fun loadCircle(context: WeakReference< context >, URL: String? , image: ImageView? , width_height: Int) {if (image == null) return} Open fun loadRound(Context: WeakReference< context >, URL: String, image: ImageView? , width: Int, height: Int, round: Int) {if (image == null) return} open fun clearCache(context: WeakReference<Context>) {// Force clear cache, can be memory cache or hard disk cache Glide example: Glide. .applicationContext).clearMemory() System.gc() } }Copy the code
Directions for use
ImageLoad().loadCircle(WeakReference(mContext), remark.user_info.portrait, ImageLoad().load(WeakReference(mContext), news.image_1, holds.imageView, width, WeakReference(mContext), WeakReference(mContext), holder. Height) ImageLoad().loadround (WeakReference(mContext), briefCard["icon"].toString(), holder.image, 5)
Copy the code
Glide gives us an API to reduce unnecessary memory wastage when lists are loaded. Take recyclerView as an 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() } } } })Copy the code
We can call the pauseRequests method to suspend the image loading during the list sliding process, and call resumeRequests to resume the loading when the sliding is over. Will, of course, want to reduce the memory overhead application can also be called ImageLoad () clearCache (WeakReference ([email protected])) to empty Glide memory cache
The specific use method and the use details see imitation open eye video Android client welcome everyone more star.