About the Glide

Glide is a fast and efficient Image loading library for Android that focuses on smooth scrolling. Glide provides an easy-to-use API, a high-performance, scalable decode pipeline, and automated resource pooling technology.

Glide supports pulling, decoding, and displaying video snapshots, pictures, and GIF animations.

Glide downloads images using the HttpUrlConnection class by default, but it also provides a library of tools that integrate quickly with Google Volley and Square OkHttp.

Although Glide’s primary goal is to make scrolling through any kind of list of images as fast and smooth as possible, Glide can actually do almost anything you need to pull/zoom/display remote images.

The Android SDK requirements

  • To use Glide, the minimum SDK version (minSdkVersion) must be API 14 (Ice Cream Sandwich) or higher.
  • The SDK version to be compiled using Glide must be API 27 (Oreo MR1) or higher

Android Studio online dependency

Glide supports online dependencies. The latest version is 4.8.0. Add two lines of code under the Dependencies property in the build.gradle file.

Dependencies {the compile 'com. Making. Bumptech. Glide: glide: 4.8.0' annotationProcessor 'com. Making. Bumptech. Glide: the compiler: 4.8.0'}Copy the code

permissions

Glide normally needs to add the following four permissions to the Androidmanifest.xml file:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />Copy the code
  • INTERNET: Access to the network that allows you to load images online from the network.
  • ACCESS_NETWORK_STATE: Access to network state. Not required, but capable of handling flaky network and airplane mode.
  • READ_EXTERNAL_STORAGE: reads the permission of the local SD card and loads images from the local folder, DCIM, or gallery.
  • WRITE_EXTERNAL_STORAGE: This permission is required to store Glide’s cache on an SD card.

Simple to use

Glide loading images is very simple and requires only one line of code:

Glide.with(this)
    .load("https://github.com/ansen666/images/blob/master/" +
      "public/qrcode_for_gh_14a89f21bd5e_258.jpg?raw=true")
    .into(imageView);Copy the code

The above line calls three methods in sequence:

  • Pass in an Activity or Fragment instance with this
  • The load picture url
  • The into image is displayed on the control after loading

It is possible to cancel loading, but it is not normally required because Glide unloads and recycles resources when an Activity or Fragment instance passed in the Glide. With () method is destroyed.

A background thread

In fact, Glide can obtain Bitmap through the background thread, in the background thread directly use submit(int, int) method.

new Thread(runnable).start(); Runnable =new Runnable() {@override public void run() {FutureTarget< bitmap > FutureTarget = Glide.with(MainActivity.this) .asBitmap() .load("https://github.com/ansen666/images/blob/master/" + "public/qrcode_for_gh_14a89f21bd5e_258.jpg?raw=true") .submit(); try { Bitmap bitmap = futureTarget.get(); Log. I ("ansen"," obtain bitmap:"+bitmap); Glide. With (mainActivity.this). Clear (futureTarget); } catch (ExecutionException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); }}};Copy the code

First, create a new thread. The difference is that we call the submit method, which can pass in a width and height or not, and return the FutureTarget object. After calling the GET method to get the Bitmap we need, when the Bitmap we get is used up, Remember to call the clear method to free resources.

The futureTarget.get method throws an exception that needs to be caught with a try catch.

Of course, if you don’t want to open a thread to get a Bitmap, you can use the main thread to get a Bitmap asynchronously.

Glide.with(this) .asBitmap() .load("https://github.com/ansen666/images/blob/master/" + "public/qrcode_for_gh_14a89f21bd5e_258.jpg?raw=true") .into(new SimpleTarget<Bitmap>() { @Override public void OnResourceReady (Bitmap resource, Transition<? Super Bitmap> Transition) { Imageview.setimagebitmap (resource); } @Override public void onLoadFailed(@Nullable Drawable errorDrawable) { super.onLoadFailed(errorDrawable); Log. I ("ansen", "image failed to load "); }});Copy the code

As you can see from the above code, the into method passes in a SimpleTarget object, which is an abstract class. This class is implemented as an inner class, overriding the two methods of this class (onResourceReady and onLoadFailed). OnResourceReady successfully loads the image and returns the Bitmap as an argument. The onLoadFailed method failed to load the image callback.

The load method passes in additional parameters

Glide can load Resources, files, URIs, byte arrays, and so on.

Glide.with(context).load(resourceId).into(imageView);
Glide.with(context).load(file).into(imageView);
Glide.with(context).load(uri).into(imageView);
Glide.with(context).load(byte[]).into(imageView);Copy the code

Loading GIF images

Glide supports direct loading of Gif images, it is also very simple to use, just call an asGif method

Glide.with(this).asGif().load(R.mipmap.result).into(ivGif);Copy the code

Generated API

Glide V4 uses the Annotation Processor to generate an API that can be used in the Application module to make a one-time call to the RequestBuilder. RequestOptions and all options in the integration library.

The Generated API mode is designed for two purposes:

  • The integration library extends custom options for the Generated API.
  • In the Application module, groups of commonly used options can be packaged into a single option for use in the Generated API

Begin to use

The Generated API is currently only available within the Application module. This restriction allows you to have only one copy of the Generated API, rather than each Library and Application having its own defined Generated API.

This makes calling the Generated API easier and ensures that the options for calling the Generated API in the Application module behave consistently throughout. This restriction may be removed in a later release (given experimentally or otherwise).

To use the Generated API in the APP module, there are two steps:

1. Add Glide processor dependencies

Dependencies {annotationProcessor 'com. Making. Bumptech. Glide: the compiler: 4.8.0'}Copy the code

We included this at the beginning of this article when we covered Android Studio’s online dependencies, so this step can be ignored.

The Application module contains an implementation of the AppGlideModule

Let’s create a new class called MyAppGlideModule. You can call it anything, but it must inherit from AppGlideModule and add the @glidemodule annotation tag to the class.

import com.bumptech.glide.annotation.GlideModule; import com.bumptech.glide.module.AppGlideModule; /** * @author ansen * @create time 2018/10/28 */ @GlideModule public final class MyAppGlideModule extends AppGlideModule {}Copy the code

Android Studio handles the Annotation Processor and generated API correctly most of the time. However, when you add AppGlideModule for the first time, you need to rebuild the project. Otherwise, using the GlideApp class will prompt you that the class cannot be found.

Using the Generated API

The Generated API is named GlideApp by default, and it is used the same as GlideApp before, just replace GlideApp with GlideApp, for example, load online image display, the code is as follows:

/ / Generated API usage GlideApp. With (this). Load (+ "https://github.com/ansen666/images/blob/master/" "public/qrcode_for_gh_14a89f21bd5e_258.jpg?raw=true") .into(imageView);Copy the code

Unlike Glide. With (), options such as fitCenter() and placeholder() are available directly in the Builder without passing in a separate RequestOptions object.

Say something nonsense

Glide today to introduce you here, next I will explain Glide placeholder, animation, Target, configuration, cache, integration of Okhttp and a series of articles, I hope you continue to pay attention to the new week has begun, I wish you a happy work.

Source code download:

https://github.com/ansen666/GlideTestCopy the code

If you want to see my later article for the first time, scan code to pay attention to the public number, long-term push Android development articles, the latest developments, open source projects, so that you all kinds of posture.

Android Development 666 - Android development technology share scan qr code plus attentionCopy the code

Use of the Glide Open Source library