Glide is a fast and efficient multimedia management and image loading framework, encapsulating the Android platform multimedia decoding, memory and hard disk caching, etc., Glide supports decoding, display video, image and GIFs, Glide is based on custom HttpUrlConnection, download and use

  • jar

    Can be found inGithubDownload the latest JAR packageDownload address
  • Gradle

    repositories { mavenCentral() // jcenter() works as well because it pulls from Maven Central } dependencies { compile 'com. Making. Bumptech. Glide: glide: 3.7.0' compile 'com. Android. Support: support - v4:19.1.0'}Copy the code
  • Maven
    Com. Making. Bumptech. Glide glide 3.7.0 com. Google. Android support - v4 r7Copy the code
    • confusion
      -keep public class * implements com.bumptech.glide.module.GlideModule
      -keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
      **[] $VALUES;
      public *;
      }
      -keepresourcexmlelements manifest/application/meta-data@value=GlideModuleCopy the code

Loading pictures

The most basic use

  image = (ImageView)findViewById(R.id.image);
  Glide.with(this)
        .load("http://ww4.sinaimg.cn/large/610dc034gw1f96kp6faayj20u00jywg9.jpg")
        .into(image);Copy the code

With (Context Context):Context supports Activity Context Fragment FragmentActivity load(): supports remote images, local image files, image resources, Multimedia database URI into(): which ImageView to display in

  • Loading resource images
    Glide.with(this).load(R.drawable.ic_launcher).into(image);Copy the code
  • Loading file image
    File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/DCIM/Camera/IMG_20151025_120635_HDR.jpg");
    Glide.with(this).load(f).into(image);
    //or uri
    Glide.with(this).load(Uri.fromFile(f)).into(image);Copy the code
  • Load network picture, need to set up network permission, and network
    Glide.with(this)
        .load("http://ww4.sinaimg.cn/large/610dc034gw1f96kp6faayj20u00jywg9.jpg")
        .into(image);Copy the code

Set up the placeholder map and animation

If the image is too large or the Internet is too slow, you can set up a placeholder for the image to display before the image loads. You can also set up a placeholder to display if the image fails to load

Glide.with(this).load("http://ww4.sinaimg.cn/large/610dc034gw1f96kp6faayj20u00jywg9.jpg") .placeholder(r.rawable.ic_launcher) // Set the placeholder map to display.error(r.rawable.icon) before loading; // Display.into(image) when the image fails to load;Copy the code

When the image is loaded, you can also set the animation that the image displays

Glide.with(this).load("http://ww4.sinaimg.cn/large/610dc034gw1f96kp6faayj20u00jywg9.jpg") .placeholder(r.rawable.ic_launcher).error(R.rawable.icon).crossfade () // Set display animation,.into(image);Copy the code

CrossFade () has several overloaded methods crossFade(int duration): CrossFade (int animationId, int duration): Loads animation resources and time

Image clipping and scaling

Glide provides the Override (horizontalSize, verticalSize) method to crop the image to the set size. Glide also provides two methods centerCrop() and fitCenter to scale and display images equally. Try the difference between the two methods

  Glide.with(this)
        .load("http://ww4.sinaimg.cn/large/610dc034gw1f96kp6faayj20u00jywg9.jpg")
        .override(200, 240)
        .centerCrop()
        .into(image);Copy the code
  Glide.with(this)
        .load("http://ww4.sinaimg.cn/large/610dc034gw1f96kp6faayj20u00jywg9.jpg")
        .override(200, 240)
        .fitCenter()
        .into(image);Copy the code

Display GIFs and videos

Glide support to display GIFs and video, video loading and image loading is basically the same, display GIFs just call method asGif()

  Glide  
    .with( context )
    .load( gifUrl )
    .asGif()
    .error( R.drawable.full_cake )
    .into( imageViewGif );Copy the code

It is also possible to display only GIFs static images

  Glide  
    .with( context )
    .load( gifUrl )
    .asBitmap()
    .into( imageViewGifAsBitmap );Copy the code

The cache

We all know that image processing and display is the most memory consuming in Android, which is easy to cause OOM problems. Generally, image processing and display will use cache strategy, memory cache or hard disk cache. Glide also provides different cache strategy, by default, will display image memory cache. It is also possible to set not to use memory cache by calling skipMemoryCache(true) to tell Glide that we do not intend to use memory cache, which is used by default

  Glide.with(this)
        .load("http://ww4.sinaimg.cn/large/610dc034gw1f96kp6faayj20u00jywg9.jpg")
        .skipMemoryCache(true)
        .into(image);Copy the code

Glide also provides disk caching, the strategy of which can be set using the diskCacheStrategy() method

Glide.with(this) .load("http://ww4.sinaimg.cn/large/610dc034gw1f96kp6faayj20u00jywg9.jpg") .diskCacheStrategy(diskCacheStrategy.none) // Do not use disk cache. Into (image);Copy the code

Hard disk caching policy DiskCacheStrategy. NONE: do not use the hard disk cache DiskCacheStrategy. SOURCE: will DiskCacheStrategy in the original image is cached in the hard disk. The RESULT: Caches images of the displayed size to disk (default cache policy) diskCacheStrategy. ALL: The displayed image and the original image are cached

Request priority

Glide can set the image loading priority, this priority strategy is not strictly implemented, just a guide strategy, from low to high priority, you can set different priority for different images, see the effect of loading

  Priority.LOW
  Priority.NORMAL
  Priority.HIGH
  Priority.IMMEDIATECopy the code
  Glide.with(this)
        .load("http://ww4.sinaimg.cn/large/610dc034gw1f96kp6faayj20u00jywg9.jpg")
        .priority(Priority.HIGH)
        .into(image);Copy the code

Custom transformations and animations

Glide provides a way for developers to customize transformations (rounded images, image blurs, etc.) and animations