In the passed Google Developer Summit Thailand, Google introduced us a Image Loader Library for Android developed by Bump Tech named Glide as a library that recommended by Google. It has been used in many Google open source projects till now including Google I/O official application.
It succeeded in making me interested. I spent a whole night playing with it and decided to share my experience in this blog post. As a begining, I must say that it looks 90% similar to Picasso. To was more precise, I think it was something like a picasso-clone.
Anyway it is quite different in details. You'll learn how.
Import to Project
Both Picasso and Glide is on Jcenter. Can simply import it to your project with dependency like this:
Picasso
| 123 |
dependencies { compile ‘com.squareup.picasso:picasso:2.5.1‘} |
Glide
| 1234 |
dependencies { compile ‘com.github.bumptech.glide:glide:3.5.2‘ compile ‘com.android.support:support-v4:22.0.0‘} |
Anyway Glide also needs Android support Library v4, please don ' t forget to import to support-v4 your project like above as well . But it isn't kind of a problem since Android support Library V4 are basically needed in every single new-age Android Proje Ct.
Basic
As I said, it is very similar to Picasso. The ' to ' load an image to ImageView with Glide is quite the same as Picasso.
Picasso
| 123 |
Picasso.with(context) .load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg") .into(ivImg); |
Glide
| 123 |
Glide.with(context) .load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg") .into(ivImg); |
Although it looks quite the same but in details Glide was designed far better since with doesn ' t accept only Context but a LSO Activity and Fragment. Context would be automatically extracted from those things to throw in.
And the brilliant benefit from passing activity/fragment to Glide is:image loading would being integrated with Activity/frag ment ' s lifecycle for example, pause loading in Paused state and automatically resume on resumed state. So I encourage your to pass the Activity or Fragment to Glide not just a Context if possible.
Default Bitmap Format is rgb_565
Here are the result of the image loading comparing to Picasso. (1920x1080 pixels image is loaded to 768x432 pixels ImageView)
You can notice this image loaded by Glide have the worse quality compared to Picasso. Why? This is because Glide default Bitmap Format was set to RGB_565 since it consumed just 50% memory footprint compared to ARGB_8888 .
Here are the memory consumption graphs between Picasso at ARGB8888 and Glide at RGB565. (Base application consumes around 8MB)
You don ' t has to does anything if you're OK with the image ' s quality already. But if think it was unacceptable or just not good enough for you, you can switch Bitmap Format to by ARGB_8888 creating a new C Lass which extended from GlideModule :
| 12345678910111213 |
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. }} |
And then define it as meta-data insideAndroidManifest.xml
| 12 |
<meta-dataandroid:name="com.inthecheesefactory.lab.glidepicasso.GlideConfiguration" android:value="GlideModule"/> |
It looks far better now!
Let's take a look at memory consumption graphs once again. It appears that although Glide consumes almost 2 times than previous but Picasso still consumes a lot memory footprint Mor E than Glide.
The reason is Picasso loads the full-size image (1920x1080 pixels) to the memory and let GPU does the real-time Resiz ing when drawn. While Glide loads the exact imageview-size (768x432 pixels) into the memory which are a best practice. Anyway you can change the behavior of Picasso to do the same with resize () command:
| 1234 |
picasso.with ( this &NBSP;&NBSP;&NBSP;&NBSP; .load ( "http://nuuneoi.com/uploads/source/playstore/cover.jpg" ) &NBSP;&NBSP;&NBSP;&NBSP; .resize ( 768 432 " &NBSP;&NBSP;&NBSP;&NBSP; .into ( Ivimgpicasso); |
But the problem are need to manually calculate the ImageView ' s size. Or If your ImageView has the exact size (not set to wrap_content), and you can simply does like this.
| 12345 |
Picasso.with(this) .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg") .fit() .centerCrop() .into(ivImgPicasso); |
Memory consumption graphs is now finally almost the same!
Although memory consumption is quite the same but I must say which Glide beats Picasso in term of functionality of this PA RT since it could calculate the ImageView size automatically in every single case.
Image ' s quality in details
Here's the result when I tried to zoom a ImageView to the actual size.
It is noticeable this image loaded by Glide have some hard pixels and are not as smooth as the Picasso one. And till now, I still couldn ' t find the straight-to-change image resizing algorithm.
But if you ask me am it bad? I would say that it's not this noticeable in real use. Quality is acceptable and you just need to set Bitmap Format to argb_8888, that's all.
Disk Caching
Default disk caching concept of Picasso and Glide are quite different. From the experiment, the same full HD image is loaded to ImageView with Picasso and Glide. When I checked the cache folder, it appears this Glide cached the imageview-size (768x432 pixels) while Picasso cached the Full-size one (1920x1080 pixels).
And yes, hard pixels described above is also there. In addition, if image was loaded in RGB565 mode, the cached image would be also in RGB565.
When I tried to adjust ImageView to the different sizes. The result is whatever the size is, Picasso would cache only a single size of image, the full-size one. Glide acts differently, caches separate file for each size of ImageView. Although an image have already been loaded once if you need to load another size the same image, it needs to be Downloa Ded once again before is resized to the right resolution and then being cached.
To being more clear, if there is a ImageView in the first page with 200x200 pixels dimension and there are the another one in The second page with 100x100 pixels is needed to show the same image. You have to download the same image twice.
Anyway you could adjust it behavior by let Glide cache both the full-size image and the resized onewith this command.
| 1234 |
Glide.with(this) .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg") .diskCacheStrategy(DiskCacheStrategy.ALL) .into(ivImgGlide); |
The next time image is requested to show on any ImageView, the full-size image would being loaded from cache, resized and the N Cached.
An advantage of the-Glide was designed are image could be loaded and showed very fast. While the Picasso-causes some delay on loading since it needs to be resized first before are set to an ImageView even Y ou add this command to make it showed immediately.
There is some trade off between Picasso ' s and Glide ' s by disk caching. You can choose the the-the-your app ' s requirement best.
For me, I prefer Glide to Picasso since it was far from faster although it needs more space to the cache the image.
Features
You can does almost all the same things just like Picasso can does with the same style of coding for example, Image resizing
| 12345 |
// Picasso.resize(300, 200);// Glide.override(300, 200); |
Center cropping
| 12345 |
// Picasso.centerCrop();// Glide.centerCrop(); |
Transforming
| 12345 |
// Picasso.transform(newCircleTransform()) // Glide.transform(newCircleTransform(context)) |
Setting the Placeholder and Error image
| 1234567 |
// Picasso.placeholder(R.drawable.placeholder).error(R.drawable.imagenotfound)// Glide.placeholder(R.drawable.placeholder).error(R.drawable.imagenotfound) |
As I said, if you is familiar with Picasso, moving to Glide would is just like chewing a candy for you. =)
What's that Glide have but Picasso doesn ' t
An ability-to-load GIF Animation to a simple ImageView might is the most interesting feature of Glide. And yes, you can ' t do and Picasso.
And since Glide is designed to work perfectly with Activity/fragment's lifecycle so the animation would be automatically p Aused and resumed along with Activity/fragment ' s state.
The Glide caches is still are the same, resized first and then cached.
Anyway from the measurement I found that GIF Animation consumes quite a lot of memory. Please use it wisely.
Besides GIF Animation loading, Glide is also able to decode any local video file to a still image.
Another feature that might be useful are you can configure the the-the-the-loop image appears with a Animator (r.animator) while Picass O could do with one animation, fading in.
The last one if you could generate a thumbnail file of an image you loaded with thumbnail() .
Actually there is some other features you can play with but the most of the them is not that important for general use for Examp Le, transcode an image into Byte Array, etc.
Configurations
You can adjust so many configurations for example, size and location of disk caching, maximum limit of memory caching, Bit Map Format and many more. You can read more about the this at the Configuration page.
Library ' s size
Picasso (v2.5.1) ' s size is around 118KB while Glide (v3.5.2) 's is around 430KB.
Anyway 312KB difference might not being that significant.
Method count of Picasso and Glide is at 840 and 2678 respectively.
I must say 2678 is quite a lot for 65535 methods limit of Android DEX file. Proguard is recommended to turn on if you choose Glide. (And you should turn it in anyway for production release).
Conclusion
Neither Glide nor Picasso is perfect. The Glide loads an image to memory and does the caching is better than Picasso which let an image loaded far faster. In addition, it also helps preventing a app from popular OutOfMemoryError. GIF Animation loading is a killing feature provided by Glide. Anyway Picasso decodes an image with better quality than Glide.
Which one do I prefer? Although I use Picasso for such a very long time, I must admit that I now prefer Glide. But I would recommend the change Bitmap Format to argb_8888 and let Glide cache both full-size image and resized one fi Rst. The rest would do your job great!
Resources
There is many online resources related to Glide. But here is what I found. Please take a look at links below.
-Glide 3.0:a Media Management library for Android
-Glide Wiki
-Android Picasso vs Glide
-Android:image Loading libraries Picasso vs Glide
Introduction to Glide, Image Loader Library for Android, recommended by Google