作者: weiyf
时间: 2016-10-31 10:32:31
原文链接:https://futurestud.io/tutorials/glide-getting-started

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)?

有经验的Android开发者可以忽略本章节,但是对于初学者来说:也许可能会问自己,为什么想要去用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.

在这个系列中,我们看到了很多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)

希望现在我们已经说服你去使用这样的一个库来处理你的图片加载请求。如果你想要了解更多关于Glide,这就是你指南!

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!

首先第一件事,添加Glide到你的依赖,截至发稿时Glide的最新版本为3.7.0

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

Gradle

与大多数依赖一样,添加下面的一行在你的Gradle项目中的build.gradle

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'

Maven

虽然现在我们的项目基本上都是基于Gralde,但是Glide也支持Maven项目:

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

<dependency>  
<groupId>com.github.bumptech.glide</groupId>
<artifactId>glide</artifactId>
<version>3.7.0</version>
<type>aar</type>
</dependency>

初体验: 从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";

Glide
.with(context)
.load(internetUrl)
.into(targetImageView);

是的!如果你的图片URL是存在并且可用的并且你的ImageView是处在显示状态的时候,你将会在几秒后看到你的图片。以防图片不存在,Glide会返回一个错误回调,这个我们往后再看。你可能已经被这三行Glid的代码说服它是对你有用的,但还是它特性的冰山一角。

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.

展望(Outlook)

下一篇博客中,我们将会开始看看除了从网络URL中加载的其他选项。总的来说,我们将会从Android资源,本地文件,一个Uri加载一张图片。