Glide 4 basic use

This is the introduction of Chinese Glide v4: Click to open the link

1. Basic use


2. Occupation map

RequestOptions options = new RequestOptions()


Images that appear when the network is loaded

3. Specify picture size

RequestOptions options = new RequestOptions()

override the first parameter multiplied by the second parameter represents the size. This is not recommended because the setting is too large

4. Caching mechanism

        RequestOptions options = new RequestOptions()
                //Exception bitmap (image that appears when loading exception)
                //Disable Glide hard disk cache

        //Glide is divided into memory cache and hard disk cache
        //Memory cache: the main function of memory cache is to prevent applications from repeatedly reading image data into memory
        //Hard disk cache: the main function of hard disk cache is to prevent applications from repeatedly downloading and reading data from the network or other places

        //Memory cache
        //You know, by default, Glide automatically turns on memory caching.
        // In other words, when we use Glide to load an image, the image will be cached in memory,
        // As long as it is not cleared from memory, the next time you use Glide to load this image, it will be read directly from memory,
        // Instead of reading from the network or hard disk again, the loading efficiency of pictures can be greatly improved.
        // Let's say you slide up and down in a recycler view,
        // Any image loaded by Glide in RecyclerView can be read and displayed directly from memory,
        // This greatly improves the user experience.

        //Memory cache is enabled by Glide by default
        //If you want to disable memory caching for some special reasons
//        RequestOptions options = new RequestOptions()
//            .skipMemoryCache(true);
        //Where setting true disables memory caching

        //Hard disk cache
        //Disable hard disk caching at the beginning of 4
        //Five parameters of diskCacheStrategy()
        //DiskCacheStrategy.NONE: indicates that nothing is cached.
        //DiskCacheStrategy.DATA: indicates that only the original image is cached.
        //DiskCacheStrategy.RESOURCE: indicates that only converted pictures are cached.
        //DiskCacheStrategy.ALL: indicates to cache both the original image and the converted image.
        //DiskCacheStrategy.AUTOMATIC: it means that Glide can intelligently choose which caching strategy to use according to the picture resources (the default option).

5. Specify the loading format

//        Automatically determine image format

//        Specify picture format
                //This method means that only static images are allowed to load here
                //Show the first frame if it's in Gif format
                //Glide 4 has four kinds of asBitmap asDrawable asFile asGif

6. Callback and monitoring

 //The into() method does not have to pass in ImageView only. SimpleTarget is required

        SimpleTarget<Drawable> simpleTarget = new SimpleTarget<Drawable>() {
            public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {


        //Here we create an instance of SimpleTarget and specify that its generic type is Drawable,
        // Then override the onresourceready () method. In the onResourceReady() method,
        // We can get the image object loaded by Glide, that is, the Drawable object passed from the method parameter.
        // With this object, you can use it for any logical operation. Here I simply display it on the ImageView.
        //The SimpleTarget implementation is created, so you only need to pass it into the into() method when loading the image.

7. Preloading


        //Pre load before
        //Call directly when you want to use it

8. submit() method to download pictures

new Thread(new Runnable() {
            public void run() {

                try {
                    final Context context = getApplicationContext();
                    FutureTarget<File> target = Glide.with(context)
                    final File imageFile = target.get();
                    //In the main thread
                    runOnUiThread(new Runnable() {
                        public void run() {

                } catch (InterruptedException e) {
                } catch (ExecutionException e) {


9. listener() method listens to Glide's loading picture status

                .listener(new RequestListener<Drawable>() {

                    //Return false to indicate that the event has not been processed and will continue to be passed down,
                    // Return true to indicate that the event has been processed and will not be passed down.
                    // For a simple example, if we use the onResourceReady() method of RequestListener
                    // If true is returned in, the onResourceReady() method of Target will not be called back.

                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        //The onLoadFailed() method will be called back when the image fails to load
                        //Judged by GlideException
                        return false;

                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        //When the image is loaded, the onResourceReady() method will be called back
                        return false;

10. Image transformation (deformation)

RequestOptions options = new RequestOptions()
                //Turn round
//                .circleCrop();

                //To become other, you need to add another dependency
                //compile 'jp.wasabeef:glide-transformations:3.0.1'

//                .transform(new BlurTransformation());
                //black and white
                .transform(new GrayscaleTransformation());


11. Glide saves pictures locally

private void downLoadImageNew() {

        final Context context = getApplicationContext();

        RequestOptions options = new RequestOptions()
                .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL); //Specify picture size (original)

                .into(new SimpleTarget<Bitmap>() {
                    public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {

                        Bitmap tempBitmip = resource;
                        //Convert timestamps to fixed format
                        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
                        Date date = new Date(System.currentTimeMillis());
                        //Picture name
                        String mFileName = "/bzf/" + format.format(date) + ".jpg";
                        //Picture path
                        File tempPath = Environment.getExternalStorageDirectory();
                        //Save picture
                        File newFile = saveBitmap(tempPath, mFileName, tempBitmip);
                        String backgroundPath = newFile.getPath();
                        if (backgroundPath != null) {

                            // Next, insert the file into the system library
                            try {
                                        newFile.getAbsolutePath(), backgroundPath, null);
                            } catch (FileNotFoundException e) {

                            // Finally inform the library to update
                            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                                    Uri.fromFile(new File(newFile.getPath()))));

                            Toast.makeText(context, "Picture saved to:/storage/emulated/0/bzf", Toast.LENGTH_SHORT).show();

                        } else {
                            Toast.makeText(context, "Save failed", Toast.LENGTH_SHORT).show();


OK. I feel that these are enough for the time being. I will update the new ones next time.

Tags: network

Posted on Thu, 30 Apr 2020 12:11:09 -0700 by ghostrider1