【雙語】Glide — 入門(Glide — Getting Started)

作者: weiyf
時(shí)間: 2016-10-31 10:32:31
原文鏈接:https://futurestud.io/tutorials/glide-getting-started

Glide, 就像Picasso一樣, 可以從多種來源加載和顯示圖片,而且也會(huì)去兼顧緩存和在做圖片處理的時(shí)候維持一個(gè)低內(nèi)存消耗的狀態(tài)。這個(gè)庫已經(jīng)在Google官方app中使用(eg: Google I/O 2015),和Picasso一樣受歡迎。在這個(gè)系列中,我們準(zhǔn)備去探索Glide相對(duì)于Picasso之間的不同以及優(yōu)勢。

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 — 高級(jí)加載(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)?

有經(jīng)驗(yàn)的Android開發(fā)者可以忽略本章節(jié),但是對(duì)于初學(xué)者來說:也許可能會(huì)問自己,為什么想要去用Glide,而不是自己去實(shí)現(xiàn)一個(gè)。

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 在處理圖像的時(shí)候顯得有點(diǎn)耍大牌,因?yàn)樗麜?huì)以像素點(diǎn)的形式(pixel by pixel)加載到內(nèi)存之中。對(duì)于手機(jī)攝像頭來說平均一張普通的照片尺寸為2592x1936像素(5百萬像素)會(huì)分配19MB的內(nèi)存。對(duì)于復(fù)雜的且參差不齊的網(wǎng)絡(luò)環(huán)境,圖片緩存和圖片處理,如果你使用一個(gè)像Glide那樣開發(fā)和測試完善的庫,會(huì)省掉你好多時(shí)間和不會(huì)讓你頭痛。

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.

在這個(gè)系列中,我們看到了很多Glide特性。只要看看這博客的文章提綱概要,然后想想你是否真的要自己去開發(fā)所有的這些功能。

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)

希望現(xiàn)在我們已經(jīng)說服你去使用這樣的一個(gè)庫來處理你的圖片加載請(qǐng)求。如果你想要了解更多關(guān)于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到你的依賴,截至發(fā)稿時(shí)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

與大多數(shù)依賴一樣,添加下面的一行在你的Gradle項(xiàng)目中的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

雖然現(xiàn)在我們的項(xiàng)目基本上都是基于Gralde,但是Glide也支持Maven項(xiàng)目:

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>  

初體驗(yàn): 從URL加載圖片(First Peek: Loading Image from a URL)

就像Picasso一樣,Glide庫是使用流接口( fluent interface)。Glide對(duì)于一個(gè)完成的請(qǐng)求,它的建造者最少需要三個(gè)參數(shù):

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) - 對(duì)于很多Android API 來說,Context這個(gè)參數(shù)是必須的。當(dāng)然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) - 這里指定你哪張圖片 需要被加載。很多情況下,它會(huì)是一個(gè)網(wǎng)絡(luò)圖片的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.

紙上談兵總是難以掌握的,所以我們要看一個(gè)實(shí)際動(dòng)手的例子:

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是處在顯示狀態(tài)的時(shí)候,你將會(huì)在幾秒后看到你的圖片。以防圖片不存在,Glide會(huì)返回一個(gè)錯(cuò)誤回調(diào),這個(gè)我們往后再看。你可能已經(jīng)被這三行Glid的代碼說服它是對(duì)你有用的,但還是它特性的冰山一角。

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)

下一篇博客中,我們將會(huì)開始看看除了從網(wǎng)絡(luò)URL中加載的其他選項(xiàng)??偟膩碚f,我們將會(huì)從Android資源,本地文件,一個(gè)Uri加載一張圖片。

轉(zhuǎn)載請(qǐng)注明出處:http://weiyf.cn/2016/10/31/Glide-—-Getting-Started/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,640評(píng)論 5 6
  • 前年家里添了一枚萌寶,日子過得真快,才兩年多的樣子,小寶寶現(xiàn)在已經(jīng)由一個(gè)抱在懷里一點(diǎn)點(diǎn)大的小娃娃,長成了一個(gè)會(huì)...
    啞藝閱讀 171評(píng)論 0 0
  • 為什么玫瑰有刺 為了讓癡人放手 為什么要有歷史 為了求索年月日的美 為什么海潮漲了又退 為了能夠說“重來” 為什么...
    陳先生后生閱讀 189評(píng)論 0 1
  • 理性人不考慮沉沒成本,不考慮過往的恩怨,只考慮邊際收益
    吾乃哲貓閱讀 212評(píng)論 0 0
  • 世界如此之大,我卻沒有一條活路,還能怎么辦呢?于是我決定跳樓,而樓又是如此之高,我站在上面有種眩暈的感覺,還能怎么...
    半朽閱讀 924評(píng)論 29 46

友情鏈接更多精彩內(nèi)容