Glide use of the detailed

Source: Internet
Author: User

What is glide?

Glide is a library that loads pictures, and the author is Bumptech, which Google introduced to us at the Google Developer Forum in Thailand, which is widely used in Google's open source projects.

What problem does glide solve?

Glide is a very mature image loading library, he can load pictures from multiple sources, such as: Network, local, URI, etc., more importantly, he has encapsulated a very good caching mechanism and can maintain a low memory consumption when processing pictures.

How to use glide?

In the use of glide, it is similar to the use of Picasso, and their operating mechanism has a lot of similarities, many blog post will compare the two, this article also uses the same way, by comparing the two to learn the advantages and disadvantages between them.

First, when we use the two libraries when the first step is to import the library, Picasso say, directly rely on the line, but glide to note that this library is to rely on support liberary V4, so use this library, do not forget to rely on V4 package.

Basic use

The two libraries are very similar in terms of basic usage, as shown in the following code:
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);

See no, on the surface is not very similar, in fact, they have a different place, is Picasso with can only pass in the context, and glide with may pass in the context, can also be activity or fragment, you may ask, What's the use of it? The use is that the picture can be loaded with the activity or the fragment to be consistent, not appear, activity has been paused, but the picture is still loading situation.

The default bitmap format is rgb_565

This is the result of Picasso and glide loading (the images of 1920x1080 pixels are loaded into the imageview of 768x432 pixels):

As you can see, the picture loaded by glide is not as good as the Picasso loaded picture, which is why? In fact, because the glide bitmap default format is rgb_565, and Picasso with argb_8888, so although the quality is not as good as Picasso (in fact, it is not obvious on the phone), but the rgb_565 format of the image only consumes argb_ 8888 format picture half of the memory.

Here are the memory consumption graphs between Picasso at ARGB8888 and Glide at RGB565. (Base application consumes around 8MB)
is the memory consumption comparison of Picass's ARGB8888 format picture and Glide's RGB565 format picture (the app itself accounts for approximately 8M):

If you don't have too much of a picture, you can use the default format, but if you have a higher quality requirement, you can convert the picture to ARGB8888 by inheriting the Glidemoudle, as follows:

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.  }}

It is then defined in Androidmanifest.xml:

<meta-data android:name="com.inthecheesefactory.lab.glidepicasso.GlideConfiguration"            android:value="GlideModule"/>

In the second look, is not exactly the same?

Let's take a look at the memory consumption between the two:

We found that although the image format was the same, and glide loaded almost twice times the previous memory, Picasso consumed more memory than glide.

This is because Picasso loaded the full-size picture (1920x1080 pixels) into memory, and when the drawing was made, the GPU instantly recovered to the desired size (768x432 pixels), but glide loaded the exact imageview size into memory, of course, We can manually make Picasso also load images in this way:

Picasso.with(this)    .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")    .resize(768432)    .into(ivImgPicasso);

The limitation of the above approach is that we must know the exact size of the ImageView, and if our ImageView set wrap, we cannot use the way above and need to change to the following:

Picasso.with(this)    .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")    .fit()    .centerCrop()    .into(ivImgPicasso);

Now, let's look at the memory consumption graph:

Haha, the memory consumption of the two is almost the same, but it must be said at this point glide really better than Picasso do, because glide in each case can automatically calculate the size of ImageView.

Quality details of the picture

When I resize the imageview to the size of the picture (1920x1080 pixels), let's take a look at the following image:

This time the contrast between the two pictures is more obvious, glide loaded pictures can obviously see the jagged pixels, but when the user uses the application, this is not so easy to detect, and, if you really can not tolerate this small flaw, you may be able to adjust the image format to argb_8888.

External cache

The external caching mechanisms of Picasso and glide are very different by default, and can be found in experiments (images of 1920x1080 pixels are loaded into the imageview of 768x432 pixels), glide are cached in 768x432 pixels. The Picasso cache is the entire picture (1920x1080 pixels).

If the loaded picture is RGB565 mode, the cached picture is also RGB565 mode.

When we adjust the size of the ImageView, Picasso will always cache the entire picture regardless of the size of the ImageView, and glide will be different, it will cache a picture for each imageview of various sizes, That is, whether or not your picture has been loaded, as long as the size of the ImageView is not the same, then glide will reload once, it will be downloaded from the network before the load of ImageView, and then cached.

Prevent you do not understand, to give an example, if a page imageview is 200*200 pixels, and the other page imageview is 100*100 pixels, this time want to let two imageview like the same picture, then glide need to download two pictures , and two pictures are cached.

However, we can use the following method to let glide cache full-size images, there are cached images of different sizes:

Glide.with(this)     .load("http://nuuneoi.com/uploads/source/playstore/cover.jpg")     .diskCacheStrategy(DiskCacheStrategy.ALL)     .into(ivImgGlide);

One advantage of this default caching mechanism for glide is that it can speed up the loading of pictures (which can be understood as space-time), while Picasso can cause some delay because it always needs to be resized when loaded into ImageView. It is true that there is a way to display the picture immediately (as shown below), but it is Picasso to eliminate the delay.

//Picasso.noFade();

In terms of external caching, glide and Pcasso have strengths, and you can choose the right one to use (that is, whether the control is important or time-critical for your app).

Characteristics

You can almost use glide to do all the things Pcasso can do, and their code style is very similar:

To resize a picture:

// Picasso.resize(300200);// Glide.override(300200);

Center cropping:

// Picasso.centerCrop();// Glide.centerCrop();

Graphics Transformations (transforming):

// Picasso.transform(new CircleTransform())// Glide.transform(new CircleTransform(context))

To set up placeholder images and error images:

// Picasso.placeholder(R.drawable.placeholder).error(R.drawable.imagenotfound)// Glide.placeholder(R.drawable.placeholder).error(R.drawable.imagenotfound)
What glide can do pcasso but can't do

One obvious advantage of glide is that it can load GIF pictures, you may say I use Picasso load also not error AH? You should note that GIF images loaded with Picasso are not animated, as shown below:

Because Glide is designed to combine perfectly with the life cycle of activity/fragment, GIF animations will automatically start and stop with the life cycle of the activity/fragment.

The GIF cache is the same as the general picture, and is the first time the load is resized and then cached.

However, it is important to note that by measuring, we can find that GIF images will consume very much memory, so use it with caution.

In addition to loading GIF images, glide can parse any video file into a static picture.

Another useful feature is that you can configure animations that display pictures, whereas Picasso only supports one fade-in (fading-in) animation effect.

You can also use it thumbnail() to create an image of the thumbnail (tiny) picture.

There are many features, but they are generally less common, such as converting a picture's encoding into a byte array, and so on.

Configuration

We can make adjustments to many configurations, such as the size and location of the external cache, the maximum limit of the internal cache, the format of the bitmap, etc., for more configuration, refer to the configuration page.

Size of Library

The size of the Picasso is approximately 118KB, while the glide is about 430KB.

Does the size of a library mean anything? Indeed, I do not think it means much!

Let's look at a comparison of the number of methods between the two:

It is important to note that the method in Android DEX file is limited, the maximum number of methods is 65,535, and from this point of glide, there are quite a few ways to do this, and the obfuscation also suggests confusing our project.

Summarize

Glide and Picasso are not perfect, in some ways, glide in the image of the cache is relatively good, because it is faster, in addition, it can effectively prevent oom errors, and loading GIF pictures is also a great advantage of glide, However, by default, the picture quality of Picasso is very high.

Another tip is to use glide to change the format of the picture to ARGB8888 and cache the full size and other dimensions of the picture, so that it can be better to load the picture.

Resources

Here are some resources about glide:

    • Glide 3.0:A Media Management library for Android
    • Glide Wiki
    • Android Picasso vs Glide
    • Android:image loading Libraries Picasso vs Glide

Reference Original: Introduction to Glide, Image Loader Library for Android, recommended by Google

Glide use of the detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.