1. Glide cache is divided into two types,

  • Memory cache
    • skipMemoryCache(true)
  • Disk cache
    • Diskcachestrategy. NONE Nothing is cached,
    • Diskcachestrategy. SOURCE only caches the original full resolution image
    • Diskcachestrategy. RESULT Only caches the final image, i.e., reduced resolution (or converted)
    • Diskcachestrategy. ALL Caches ALL versions of images (default behavior)

Memory Cache Glide caches image resources into memory by default when we do not want to use memory cache. SkipMemoryCache **skipMemoryCache(true) by name. Also, Glide will still use disk caching, ** to avoid repeated requests for network data.

Glide.with(this) .load("http://2f.zol-img.com.cn/product/104_1200x900/305/cevDJaCdeLQ6.gif") .override(800, .placeholder(r.map.ic_launcher).error(R.map.ic_launcher_round).into(iv_2);Copy the code

I won’t say much about disk caching, but I’ll go straight to the code. We need to choose the style you need according to the types given above.

Glide.with(this)
                .load("http://2f.zol-img.com.cn/product/104_1200x900/305/cevDJaCdeLQ6.gif")
                .override(800, 300)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher_round)
                .into(iv_2);
Copy the code

Neither type of cache uses calling two methods at the same time

Glide.with(this) .load("http://2f.zol-img.com.cn/product/104_1200x900/305/cevDJaCdeLQ6.gif") .override(800, 300).skipmemorycache (true) // skipMemoryCache. DiskCacheStrategy (diskcachestrategy.none).placeholder(r.map.ic_launcher) .error(R.mipmap.ic_launcher_round) .into(iv_2);Copy the code

2. The priority of image request is in the development process. For example, picture A is A very large picture, while picture B and C are relatively small. At this time we need to let A picture load display first. We need to use that method. This enumeration gives four different options. Here is a list of increasing priority ** by ** :

  • Priority.LOW

  • Priority.NORMAL

  • Priority.HIGH

  • Priority.IMMEDIATE

    Specific code

The image resource with the priority.high attribute is loaded first before the image resource with the priority.low attribute is loaded. There is another situation that I encountered when testing: if the image with lower priority has cache data set, when we enter this interface again for the second time, the image will be loaded first before the image without cache data set. You can test it yourself

Glide.with(this) .load("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg") .placeholder(R.drawable.ic_launcher) .error(R.mipmap.ic_launcher_round) .priority(Priority.LOW) .into(iv_1); Tv_title2.settext (" load GIF resources "); Glide.with(this) .load("http://2f.zol-img.com.cn/product/104_1200x900/305/cevDJaCdeLQ6.gif") .override(800, 300) .priority(Priority.HIGH) .placeholder(R.mipmap.ic_launcher) .error(R.mipmap.ic_launcher_round) .into(iv_2);Copy the code

3. Customizing thumbnails Overview: Thumbnails are different from placeholders mentioned in the previous blog. Placeholders must come with resources bundled with the application. Thumbnails are dynamic placeholders. It can also be loaded from the network. The thumbnail will not be displayed until the actual request is loaded or processed. If the thumbnail is for any reason, it will not replace the original image after the original image arrives. It just gets erased.

Glide. With (this). The load (" http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg "). The thumbnail (0.1 f) .into(iv_8);Copy the code

** Thumbnail (0.1F)** This property achieves the same effect as the original image 10001000, which is shrunk ten times to 100100 when the thumbnail is displayed.

The following custom thumbnail has many advantages, see the overview above.

DrawableRequestBuilder<Integer> thumbail = Glide.with(this)
                .load(R.drawable.ic_pb_default);
        Glide.with(this)
                .load("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg")
                .thumbnail(thumbail)
                .into(iv_8);
Copy the code

The difference is that the first thumbnail request is completely independent of the second original request. The thumbnail can be a different resource or image URL, you can apply different transformations to it, and so on.


Overview: Setting ImageVeiw in into() is not the last step. Imagine setting Targets in into() to accept the Bitmap of the image resource. Targets is no other callback, it returns the result after Glide has done all the loading and processing.

Private SimpleTarget SimpleTarget = new SimpleTarget<Bitmap>(200,200) {@override public void onResourceReady(Bitmap) resource, GlideAnimation<? super Bitmap> glideAnimation) { iv_9.setImageBitmap(resource); }}; Tv_title9.settext ("Glide callback: Targets"); Glide.with(this) .load("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg") .asBitmap() .into(simpleTarget);Copy the code
  • As you can see from the code, the asBitmap() method is used, mainly because the address of the resource returned in the background is of various types, such as GIF, and the SimpleTarget parameter type is of Bitmap type. Glide loaded image resources to **asBitmap()** resources.
  • JAVA/ Android syntax allows anonymous inner classes to be used in init() methods. Glide will cause the Android garbage collection mechanism to remove the anonymous inner classes in cases where the phone is low on memory before the image is requested to load. When the image is loaded, the method is not called back. So make sure that simpleTarget is declared as a field object to prevent it from being removed by the Android garbage collection mechanism.
  • The second key part is this line of Glide builders: with(context). The problem here is actually Glide: When you pass a context, such as the activity of the current application, Glide will automatically stop the request when the requested activity has stopped. This integration into the application lifecycle is usually very helpful, but sometimes it can be difficult if your target is independent of the application activity lifecycle. The solution here is to use the application context:) with (context. GetApplicationContext)). Glide kills the image request only when the application has stopped completely. Requests remember, again, if your request needs to be done outside the activity lifecycle, use code like this:
Glide.with(getApplicationContext))
                .load("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg")
                .asBitmap()
                .into(simpleTarget);
Copy the code

Of course, someone might ask, if I want to set the ImageView size. I Glide has a solution for this, Target can specify the size. To use the method, look at the code above. Look at the source code.

ViewTarget custom View this is mainly used for custom views, for example, we have a composite custom control in our application, which has an ImageView that we need to set. This is easier to implement with ViewTarget.

FutureStudioView studioView = (FutureStudioView) findViewById(R.id.custom_view); ViewTarget viewTarget = new ViewTarget<FutureStudioView, GlideDrawable>(studioView) { @Override public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) { this.view.setImage(resource.getCurrent()); this.view.setText(String.valueOf(resource.getCurrent())); }}; Glide.with(this) .load("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg") .into(viewTarget);Copy the code

If you look closely, you’ll see that we can also set the TextView in the ViewTarget. Specific yourself to test.

Load images into NotificationsIt’s the notification bar icon. Glide also provides a convenient and comfortable way to:NotificationTarget. Let’s talk about renderings.The simulator is not particularly beautiful, pay attention to this look. Don’t worry too much about the details. The specific code will have a download address.

Download the DEMO address by yourself