Usage and Skills of Glide and Recommendation of Excellent Library

  • 2021-10-25 08:02:29
  • OfStack

At present, several well-known image loading libraries are Universal-ImageLoader , Glide , Fresco , Picasso

The comparison is as follows:

The author of Universal-ImageLoader library stopped maintaining at the end of 2015. Gilde is an optimized version of Picasso, and finally Fresco of Facebook. I heard that it is extremely powerful and efficient, but the size is 4M. Finally, Glide and google are maintained. The optimized version of Picasson is simple to use, perhaps not as powerful as Fresco, but it feels that hold can live most of the projects.

Compare Picasso with Glide

1. The two are used similarly, but with () of Glide accepts not only Context, but also Activity or Fragment, and Context will be automatically obtained from them. At the same time, taking Activity/Fragment as with () parameter has the advantage that the image loading will keep the life cycle of Activity/Fragment at the same time, for example, the Paused state stops loading and automatically reloads when Resumed. Therefore, I suggest passing Activity and Fragment to Glide instead of Context. 2. Why does Glide load slightly worse picture quality than Picasso? This is because the default Bitmap format for Glide is RGB_565, which is one and a half times less memory overhead than the ARGB_8888 format. Glide can of course also be formatted through GlideModule. 3. There is a big difference in disk caching strategy between the two. The Picasso cache is full size, while the Glide cache is the same size as the ImageView. The advantage of Glide in this way is that the loading display is very fast. However, the Picasso method causes 1 delay because it needs to be resized before displaying. 4. Glide can load GIF dynamic diagrams, but Picasso cannot. 5. Picasso (v2.5. 1) is about 118KB, while Glide (v3.5. 2) is about 430KB. The number of methods for Picasso is approximately 480, whereas the number of methods for Glide is approximately 2678.

Import

Picasso and Glide are both 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:24.0.0' 
} 

The with method of Glide accepts not only Context, but also Activity and Fragment, from which Context is automatically retrieved.

1. The network loads pictures into ImageView


Glide.with(context).load(imageUrl).into(imageView);

2. When loading the network picture, because the picture can't be displayed in time during the loading process, it may be necessary to set the waiting picture at this time, through placeHolder () method


Glide.with(context).load(imageUrl).placeholder(R.mipmap.ic_launcher).into(imageView);

3. When loading the picture fails, set the picture display after loading failure by error (Drawable drawable) method:


Glide.with(context).load(imageUrl).error(R.mipmap.ic_launcher).into(imageView);

4. Image scaling, centerCrop () and fitCenter ():

1) Using centerCrop is to fill the size set by ImageView with the picture map. If Height of ImageView is match_parent, the picture will be stretched and filled


Glide.with(context).load(imageUrl).centerCrop().into(imageView);

2) Using fitCenter, that is, scaling the image so that the images are measured to be equal to or less than the boundary range of ImageView, the image will be displayed completely, but may not fill the whole ImageView.


Glide.with(context).load(imageUrl).fitCenter().into(imageView);

5. Display gif animation, and asGif () judges whether it is gif animation


Glide.with(context).load(imageUrl).asGif().into(imageView);

6. Display local video


String filePath = "/storage/emulated/0/Pictures/video.mp4";
Glide.with( context ).load( Uri.fromFile( new File( filePath ) ) ).into(imageViewGifAsBitmap );

7. Caching policy


Glide.with( context ).load(imageUrl).skipMemoryCache(true).into(imageViewInternet );// Skip memory cache 

dependencies { 
  compile 'com.github.bumptech.glide:glide:3.5.2' 
  compile 'com.android.support:support-v4:24.0.0' 
} 
0 DiskCacheStrategy.NONE Nothing is cached DiskCacheStrategy.SOURCE Just cache the original full-resolution image DiskCacheStrategy.RESULT  Only the final image is cached, that is, the reduced resolution (or converted) DiskCacheStrategy.ALL  Cache all versions of images (default behavior)

8. Priority, set the order in which pictures are loaded:


dependencies { 
  compile 'com.github.bumptech.glide:glide:3.5.2' 
  compile 'com.android.support:support-v4:24.0.0' 
} 
1

9. Get Bitmap and set CircleImageVIew to use this ImageView library


dependencies { 
  compile 'com.github.bumptech.glide:glide:3.5.2' 
  compile 'com.android.support:support-v4:24.0.0' 
} 
2

10. Load round and rounded pictures


// Circular picture 
public class GlideCircleTransform extends BitmapTransformation {
  public GlideCircleTransform(Context context) {
    super(context);
  }
  @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    return circleCrop(pool, toTransform);
  }
  private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
    if (source == null) return null;
    int size = Math.min(source.getWidth(), source.getHeight());
    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;
    // TODO this could be acquired from the pool too
    Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
    Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
    if (result == null) {
      result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
    paint.setAntiAlias(true);
    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);
    return result;
  }
  @Override public String getId() {
    return getClass().getName();
  }
}

dependencies { 
  compile 'com.github.bumptech.glide:glide:3.5.2' 
  compile 'com.android.support:support-v4:24.0.0' 
} 
4

dependencies { 
  compile 'com.github.bumptech.glide:glide:3.5.2' 
  compile 'com.android.support:support-v4:24.0.0' 
} 
5

1 Some usage skills

1. Glide0 And Glide.with(context).pauseRequests()

When the list is sliding, call pauseRequests () to cancel the request, and when the sliding stops, call resumeRequests () to resume the request. Will this be better?

2. Glide.clear()

This method can help you when you want to clear all picture loading requests.

3. ListPreloader

If you want the list to be preloaded, try the class ListPreloader.

1 Some excellent libraries based on Glide

1. glide-transformations

An transformation library based on Glide, with a variety of conversion effects such as cropping, shading, blurring, filters, etc.

2. GlidePalette

A library that can easily use Palette when Glide loads.

Summarize


Related articles: