作者: weiyf
时间: 2016-10-31 10:32:31

Glide, 就像Picasso一样, 可以从多种来源加载和显示图片,而且也会去兼顾缓存和在做图片处理的时候维持一个低内存消耗的状态。这个库已经在Google官方app中使用(eg: Google I/O 2015),和Picasso一样受欢迎。在这个系列中,我们准备去探索Glide相对于Picasso之间的不同以及优势。

Glide, just like Picasso, can load and display images from many sources, while also taking care of caching and keeping a low memory impact when doing image manipulations. It has been used by official Google apps (like the app for Google I/O 2015) and is just as popular as Picasso. In this series, we’re going to explore the differences and advantages of Glide over Picasso.

Glide系列提纲概况(Glide Series Overview):

  1. 【双语】Glide — 入门(Glide — Getting Started)
  2. 【双语】Glide — 高级加载(Glide — Advanced Loading)
  3. 【双语】Glide — 列表适配器(ListView, GridView)(Glide — ListAdapter (ListView, GridView))
  4. Glide — Placeholders & Fade Animations
  5. Glide — Image Resizing & Scaling
  6. Glide — Displaying Gifs & Videos
  7. Glide — Caching Basics
  8. Glide — Request Priorities
  9. Glide — Thumbnails
  10. Glide — Callbacks: SimpleTarget and ViewTarget for Custom View Classes
  11. Glide — Loading Images into Notifications and AppWidgets
  12. Glide — Exceptions: Debugging and Error Handling
  13. Glide — Custom Transformations
  14. Glide — Custom Animations with animate()
  15. Glide — Integrating Networking Stacks
  16. Glide — Customize Glide with Modules
  17. Glide Module Example: Accepting Self-Signed HTTPS Certificates
  18. Glide Module Example: Optimizing By Loading Images In Custom Sizes
  19. Glide — Dynamically Use Model Loaders
  20. Glide — How to Rotate Images
  21. Glide — Series Roundup

为什么要使用Glide(Why Use Glide)?


Experienced Android developers can skip this section, but for the starters: you might ask yourself why you want to use Glide* instead of your own implementation.

Android 在处理图像的时候显得有点耍大牌,因为他会以像素点的形式(pixel by pixel)加载到内存之中。对于手机摄像头来说平均一张普通的照片尺寸为2592x1936像素(5百万像素)会分配19MB的内存。对于复杂的且参差不齐的网络环境,图片缓存和图片处理,如果你使用一个像Glide那样开发和测试完善的库,会省掉你好多时间和不会让你头痛。

Android is quite the diva when working with images, since it’ll load images into the memory pixel by pixel. A single photo of an average phone camera with the dimensions of 2592x1936 pixels (5 megapixels) will allocate around 19 MB of memory. If you add the complexity of network requests on spotty wireless connections, caching and image manipulations, you will safe yourself a lot of time & headache, if you use a well-tested and developed library like Glide.


In this series, we’ll look at many of the features of Glide. Just take a peek at the blog post outline and think if you really want to develop all of these features yourself.

添加Glide到你的配置中(Adding Glide to Your Setup)


Hopefully by now we’ve convinced you to use a library to handle your image loading requests. If you want to take a look at Glide, this is the guide for you!


First things first, add Glide to your dependencies. At the time of writing, the last version of Glide is 3.7.0.



As with most dependencies, pulling it in a Gradle project is a single line in your build.gradle:

compile 'com.github.bumptech.glide:glide:3.7.0'



While we moved all our projects to Gradle, Glide also supports Maven projects:


初体验: 从URL加载图片(First Peek: Loading Image from a URL)

就像Picasso一样,Glide库是使用流接口( fluent interface)。Glide对于一个完成的请求,它的建造者最少需要三个参数:

Just like Picasso, the Glide library is using a fluent interface. The Glide builder requires at least three parameters for a fully functional request:

  • with(Context context) - 对于很多Android API 来说,Context这个参数是必须的。当然Glide也是一样。
  • with(Context context) - Context is necessary for many Android API calls. Glide is no difference here. Glide makes this very convenient by also extracting the context if you pass an Activity or Fragment object.
  • load(String imageUrl) - 这里指定你哪张图片 需要被加载。很多情况下,它会是一个网络图片的URL的字符串。
  • load(String imageUrl) - here you specify which image should be loaded. Mostly it’ll be a String representing a URL to an Internet image.
  • into(ImageView targetImageView) - targetImageView是你的图片该显示的地方。
  • into(ImageView targetImageView) - the target ImageView your image is supposed to get displayed in.


Theoretical explanations are always harder to grasp, so let’s look at a hands-on example:

ImageView targetImageView = (ImageView) findViewById(R.id.imageView);  
String internetUrl = "http://i.imgur.com/DvpvklR.png";



That’s it! If the image at the URL exists and your ImageView is visible, you’ll see the image in a few seconds. In case the image does not exist, Glide will return to the error callbacks, which we’ll look at later. You might already be convinced with this three-line example that Glide is useful to you, but this is just the tip of the feature iceberg.