Image Library Glide recommended by Google and Image Library glide loaded by google
At the Google Developer Forum held in Thailand, Google introduced us to a project named? Glide? The image loading library by bumptech. This library is widely used in google's open-source projects, including the official app released at the 2014 google I/O conference.
Undoubtedly, this library has aroused my interest. So I spent one night studying and playing with it. After analyzing its implementation principles clearly, I decided to write a blog post to share some of my experiences. Before that, I would like to say that Glide and Picasso have 90% similarity. To be precise, I think it is like a Picasso clone.
In any case, there are many differences between Glide and Picasso in terms of details. Next I will let you know the differences.
Import to Warehouse
Both Picasso and Glide are on jcenter. Adding dependencies to a project is very simple: Picasso
dependencies { compile 'com.squareup.picasso:picasso:2.5.1'}
Glide
dependencies { compile 'com.github.bumptech.glide:glide:3.5.2' compile 'com.android.support:support-v4:22.0.0'}
In any case, Glide requires the Android Support Library v4 package. Do not forget to add the dependency of the Android Support Library v4 package as the code above. However, this is not a big problem, because now Android Support Library v4 is basically the standard for every new Android project.
Basic
As I mentioned, Glide and Picasso are very similar. Glide loads images in the same way as Picasso.
Picasso
Picasso.with(context) .load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg") .into(ivImg);
Glide
Glide.with(context) .load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg") .into(ivImg);
Although the two seem very similar, Glide code is undoubtedly better designed, because the Glide with () method not only accepts Context, but also Activity and Fragment. In addition, the with () method can automatically extract the Context from the various items you put for your own use.
When Activity/Fragment is used as the with () parameter, the image loading will be consistent with the Activity/Fragment lifecycle. For example, Paused is Paused, it is automatically reloaded when Resumed is used. Therefore, we recommend that you pass Activity and Fragment to Glide instead of Context when passing parameters.
The default Bitmap format is RGB_565.
The following is a comparison between image loading and Picasso (1920x1080 pixels of images are loaded into the ImageView of 768x432)
We can see that the image quality loaded by Glide is worse than Picasso (ps: I can't see it). Why? This is because the default Bitmap format of Glide is RGB_565, which is half the memory overhead of the ARGB_8888 format. The following figure shows the memory overhead of Picasso in ARGB8888 and Glide in RGB565 (the application occupies 8 m and therefore uses 8 as the baseline ):
If you think Glide is acceptable in the default RGB_565 format, you can do nothing. However, if you find it unacceptable or your actual needs require higher image quality, you can create a GlideModule subclass like the code below, convert the Bitmap format to ARGB_8888:
public class GlideConfiguration implements GlideModule { @Override public void applyOptions(Context context, GlideBuilder builder) { // Apply options to the builder here. builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888); } @Override public void registerComponents(Context context, Glide glide) { // register ModelLoaders here. }}
Then, define GlideModule as meta-data in AndroidManifest. xml.
This looks much better.
Let's take a look at the memory overhead diagram. Although it seems that the memory overhead of this Glide is nearly twice that of the previous one, the memory overhead of Picasso is much larger than that of Glide.
However, the problem with doing so is that you need to manually calculate the ImageView size, or you have set a specific size for the ImageView. To solve this problem, you can simplify your code in Picasso:
Picasso.with(this) .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg") .resize(768, 432) .into(ivImgPicasso);
But the problem is that you need to calculate the size of the ImageView, or your ImageView size is a specific value (instead of wrap_content). You can also do this:
Picasso.with(this) .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg") .fit() .centerCrop() .into(ivImgPicasso);
Now the memory overhead of Picasso is similar to that of Glide.
Although the memory overhead is not much different, Glide wins Picasso in this case. Because Glide can automatically calculate the ImageView size.
Image Quality details
This is a comparison of how to restore the ImageView to a real hour .?
Obviously, some pixels of images loaded by Glide become blurred and seem less smooth than Picasso. Until now, I have not found a method for intuitively changing the image size.
But this is not a bad thing, because it is hard to detect.
Disk cache
The disk cache policies of Picasso and Glide vary greatly. We just did an experiment using Glide and Picasso to load the same HD image. After the experiment, I checked the cache directory and found that the size of the image cached by Glide is the same as that of ImageView, the size of the cached Picasso image is the same as that of the original image.
The above mentioned smoothness problem still exists. If the image is loaded with RGB565, the cached image is also RGB565.
I tried to adjust the ImageView to different sizes, But no matter the size of Picasso, only one full size is cached. Glide is different. It caches the Image view of each size once. Although an image has been cached once, if you want to display it again in different sizes in another place, you need to download it again and adjust it to a new size, and then cache the size.
Specifically, assume that there is a x ImageView on the first page, and there is a x ImageView on the second page. The two imageviews are supposed to display the same image, but you need to download it twice.
However, you can change this behavior so that Glide caches both the full size and other sizes:
Glide.with(this) .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg") .diskCacheStrategy(DiskCacheStrategy.ALL) .into(ivImgGlide);
The next time you load an image in any ImageView, the full-size image will be taken out of the cache, adjusted again, and then cached.
Glide has the advantage of fast loading and display. The Picasso method causes some latency due to the need to re-adjust the size before the display, even if you add this code to make it display immediately:
//Picasso.noFade();
Picasso and Glide have their own strengths in the disk cache policy. You should select the most appropriate one based on your needs.
For me, I prefer Glide because it is much faster than Picasso, although it requires more space for caching.
Features
You can do almost the same thing and code as Picasso.
Image Resizing
// Picasso.resize(300, 200);// Glide.override(300, 200);
Center Cropping
// Picasso.centerCrop();// Glide.centerCrop();
Transforming
// Picasso.transform(new CircleTransform())// Glide.transform(new CircleTransform(context))
Set bitmap or load error map:
// Picasso.placeholder(R.drawable.placeholder).error(R.drawable.imagenotfound)// Glide.placeholder(R.drawable.placeholder).error(R.drawable.imagenotfound)
As I said at the beginning of the blog post, if you are already familiar with how to use Picasso, converting from Picasso to Glide is a piece of cake for you.
What Glide can do but Picasso cannot do
Glide can load GIF dynamic images, while Picasso cannot.
Meanwhile, because the lifecycles of Glide and Activity/Fragment are the same, gif animations are automatically paused and replayed along with the Activity/Fragment status. Glide is also cached in gif. Adjust the size and then cache the image.
However, from my test results, using Glide to display animations consumes a lot of memory, so exercise caution.
In addition to gif animation, Glide can also decode any local video into a static image.
Another feature is that you can configure an animation for image display, while Picasso has only one animation: fading in.
The last one is to use thumbnail () to generate the thumbnail of the image you load.
There are still some features, but they are not very important, such as converting an image into a byte array. Configuration
There are many configurable options, such as size, cached disk location, maximum cache space, and bitmap format. You can view these Configuration items on this page.
Library size
Picasso (v2.5.1) is about KB, while Glide (v3.5.2) is about KB.
However, the 312kb gap is not very important.
The number of Picasso and Glide methods is 840 and 2678, respectively.
It must be noted that, for the limit of 65535 methods of DEX files, 2678 is a considerable number. We recommend that you enable ProGuard when using Glide.
Summary
Glide and Picasso are both perfect libraries. Glide is faster than Picasso in image loading and disk caching, and Glide is more conducive to reducing the occurrence of OutOfMemoryError. gif animation is the killer of Glide. However, the quality of Picasso images is higher. Which one do you prefer?
Although I have used Picasso for a long time, I have to admit that I prefer Glide now. I suggest using Glide, but changing the Bitmap format to ARGB_8888, allowing Glide to cache the full size and change the size at the same time.