【双语】Glide — 入门(Glide — Getting Started)

作者: 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加载一张图片。

转载请注明出处:http://weiyf.cn/2016/10/31/Glide-—-Getting-Started/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,304评论 5 6
  • 前年家里添了一枚萌宝,日子过得真快,才两年多的样子,小宝宝现在已经由一个抱在怀里一点点大的小娃娃,长成了一个会...
    哑艺阅读 130评论 0 0
  • 为什么玫瑰有刺 为了让痴人放手 为什么要有历史 为了求索年月日的美 为什么海潮涨了又退 为了能够说“重来” 为什么...
    陈先生后生阅读 152评论 0 1
  • 理性人不考虑沉没成本,不考虑过往的恩怨,只考虑边际收益
    吾乃哲猫阅读 181评论 0 0
  • 世界如此之大,我却没有一条活路,还能怎么办呢?于是我决定跳楼,而楼又是如此之高,我站在上面有种眩晕的感觉,还能怎么...
    半朽阅读 868评论 29 46