SoFunction
Updated on 2025-03-04

Summary of tips for using Glide library in Android

Introduction

At the Google Developer Forum held in Thailand, Google introduced us to an image loading library called Glide, authored 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.

/bumptech/glide

Simple use

dependencies { 
 compile ':glide:3.7.0'
} 

How to view the latest version

/#search%7Cga%7C1%7Ca%3A%22glide%22 

For detailed Glide library configuration, usage and introduction, please see here:https:///article/

introduction

So everyone knows that in Android projects, image loading is a must-have homework. After experiencing multiple third-party image loading libraries, Glide is used. It feels very useful, record the tips summarized during use.

  • AS import Glide library
  • Introduction to Glide method

AS import Glide library

dependencies { 
compile ‘:glide:3.5.2' 
compile ‘:support-v4:22.0.0' 
}

Glide use

Call the method directly where the picture needs to be loaded. existwith()In the method, the parameters can be activity, fragment and context. The advantage of taking activity and fragment as parameters is that the picture can be loaded according to the life cycle of the activity and fragment.

Basic use:

(activity).load(url).into(view);

Need to note:

Do not use Glide to load images in non-main threads. If you have to use Glide to load images in non-main thread, please change the context to getApplicationContext

Introduction to Glide extension properties

1、override(int width, int height)

Use this method to customize the image size

2、fitCenter()/centerCrop()/fitStart()/fitEnd()

Set the setScaleType of the imageview to control the size of the imageview when Glide loads the image oroveride()size load picture. Reduce the possibility of loading images OOM.

3. Image cache

Glide's image caching strategy is to process accordingly according to the imageview size, and cache images with the same size as the imageview.

How to use:

.diskCacheStrategy() 

Check the source code to get

  • caches nothing, as discussed
  • caches only the original full-resolution image. In our example above that would be the 1000x1000 pixel one caches only the original image
  • caches only the final image, after reducing the resolution (and possible transformations) caches images of the same size as the imageview after loading it into the imageview according to the URL
  • caches all versions of the image (default behavior) The default cache method will cache all sizes of the image obtained by the URL.

It is obvious that during use, it is generally consideredand. Using ALL will occupy more memory, but the same picture will display different sizes in different places, which is a network request; while using RESULT will occupy relatively less memory, but a picture will display different sizes in different places, which will request the network multiple times according to the size.

4. Placeholder map, error map display

placeholder(), default placeholder

error(), the default loading of the wrong picture displayed

5. Use Glide to load images in custom imageview

When loading a custom view using Glide, the following situations may occur:

Glide fills in the placeholder map to view the custom view. The custom view will not display the image loaded by the URL for the first time, but will display the placeholder map. You need to cancel viewing the custom view again to display correctly.

Cause: When Glide loads a custom view, you need to use the Transformations method in the Glide library to convert the custom imageview or use it in the into() method.new simpleTarget()Method to process pictures.

Solution:

a. Use the Transformations method to convert

public class BlurTransformation extends BitmapTransformation {

private RenderScript rs;

public BlurTransformation(Context context) {
 super( context );

 rs = ( context );
}

@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
 Bitmap blurredBitmap = ( .ARGB_8888, true );

 // Allocate memory for Renderscript to work with
 Allocation input = (
 rs, 
 blurredBitmap, 
 .MIPMAP_FULL, 
 Allocation.USAGE_SHARED
 );
 Allocation output = (rs, ());

 // Load up an instance of the specific script that we want to use.
 ScriptIntrinsicBlur script = (rs, Element.U8_4(rs));
 (input);

 // Set the blur radius
 (10);

 // Start the ScriptIntrinisicBlur
 (output);

 // Copy the output to the blurred bitmap
 (blurredBitmap);

 ();

 return blurredBitmap;
}

@Override
public String getId() {
 return "blur";
}

}
Glide 
.with( context ) 
.load( eatFoodyImages[0] ) 
.transform( new BlurTransformation( context ) ) 
//.bitmapTransform( new BlurTransformation( context ) ) // this would work too! 
.into( imageView1 );

b. Use new simpleTarget()

(activity).load(url).into(new SimpleTarget() { 
@Override 
public void onResourceReady(GlideDrawable resource, GlideAnimation

How to modify the Glide Bimmap format

Default Bitmap format:

RGB_565 can also use RGB_8888, but it will consume relatively memory, and the effects of these two formats do not seem to be much different on the mobile phone.

How to modify the Bitmap format:

public class GlideConfiguration implements GlideModule {

@Override
public void applyOptions(Context context, GlideBuilder builder) {
 // Apply options to the builder here.
 (DecodeFormat.PREFER_ARGB_8888);
}

@Override
public void registerComponents(Context context, Glide glide) {
 // register ModelLoaders here.
}

} 

Also in, define GlideModul as meta-data

Glide settings picture tag

During use, you want to set a tag for the imageview and then load it with Glide, but there will always be an error~ How to set a tag for the ImageView?

Solution 1: Use the setTag(int,object) method to set tag, the specific usage is as follows:

(context).load((i).getUrl()).fitCenter().into(); 
(.image_tag, i); 
(new () { 
@Override 
int position = (int) (.image_tag); 
(context, (position).getWho(), Toast.LENGTH_SHORT).show(); 
} 
}); 

Create new in the values ​​folder and add

Solution 2: After Glide 3.6.0, a new method of global settings has been added. The specific methods are as follows:

First implement the GlideMoudle interface and set the tagId of ViewTaget globally:

public class MyGlideMoudle implements GlideModule{ 
@Override 
public void applyOptions(Context context, GlideBuilder builder) { 
(.glide_tag_id); 
}

@Override
public void registerComponents(Context context, Glide glide) {

}

} 

Similarly, you need to add id below

Finally add it in the file

Some practical tips

1.(context).resumeRequests()and(context).pauseRequests()

When the list is sliding, callpauseRequests()Cancel the request, when the slide stops, callresumeRequests()Recover the request. Wouldn't this be better?

2.()

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


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

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.

Reference link

  • /p/
  • /a/anzhuokaifa/androidkaifa/2015/0327/