作者: weiyf
時間: 2016-10-31 10:32:31
原文鏈接:https://futurestud.io/tutorials/glide-getting-started
Glide, 就像Picasso一樣, 可以從多種來源加載和顯示圖片咧纠,而且也會去兼顧緩存和在做圖片處理的時候維持一個低內(nèi)存消耗的狀態(tài)蓬痒。這個庫已經(jīng)在Google官方app中使用(eg: Google I/O 2015),和Picasso一樣受歡迎漆羔。在這個系列中梧奢,我們準(zhǔn)備去探索Glide相對于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):
- 【雙語】Glide — 入門(Glide — Getting Started)
- 【雙語】Glide — 高級加載(Glide — Advanced Loading)
- 【雙語】Glide — 列表適配器(ListView, GridView)(Glide — ListAdapter (ListView, GridView))
- Glide — Placeholders & Fade Animations
- Glide — Image Resizing & Scaling
- Glide — Displaying Gifs & Videos
- Glide — Caching Basics
- Glide — Request Priorities
- Glide — Thumbnails
- Glide — Callbacks: SimpleTarget and ViewTarget for Custom View Classes
- Glide — Loading Images into Notifications and AppWidgets
- Glide — Exceptions: Debugging and Error Handling
- Glide — Custom Transformations
- Glide — Custom Animations with animate()
- Glide — Integrating Networking Stacks
- Glide — Customize Glide with Modules
- Glide Module Example: Accepting Self-Signed HTTPS Certificates
- Glide Module Example: Optimizing By Loading Images In Custom Sizes
- Glide — Dynamically Use Model Loaders
- Glide — How to Rotate Images
- Glide — Series Roundup
為什么要使用Glide(Why Use Glide)?
有經(jīng)驗的Android開發(fā)者可以忽略本章節(jié)演痒,但是對于初學(xué)者來說:也許可能會問自己亲轨,為什么想要去用Glide,而不是自己去實現(xiàn)一個鸟顺。
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)加載到內(nèi)存之中。對于手機(jī)攝像頭來說平均一張普通的照片尺寸為2592x1936像素(5百萬像素)會分配19MB的內(nèi)存讯嫂。對于復(fù)雜的且參差不齊的網(wǎng)絡(luò)環(huán)境蹦锋,圖片緩存和圖片處理,如果你使用一個像Glide那樣開發(fā)和測試完善的庫欧芽,會省掉你好多時間和不會讓你頭痛莉掂。
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特性千扔。只要看看這博客的文章提綱概要憎妙,然后想想你是否真的要自己去開發(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)說服你去使用這樣的一個庫來處理你的圖片加載請求昏鹃。如果你想要了解更多關(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ā)稿時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項目中的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)在我們的項目基本上都是基于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對于一個完成的請求癞尚,它的建造者最少需要三個參數(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)
- 對于很多Android API 來說,Context這個參數(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)
- 這里指定你哪張圖片 需要被加載熊昌。很多情況下,它會是一個網(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.
紙上談兵總是難以掌握的,所以我們要看一個實際動手的例子:
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)的時候昂利,你將會在幾秒后看到你的圖片。以防圖片不存在铁坎,Glide會返回一個錯誤回調(diào)蜂奸,這個我們往后再看。你可能已經(jīng)被這三行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)
下一篇博客中,我們將會開始看看除了從網(wǎng)絡(luò)URL中加載的其他選項襟铭÷捣睿總的來說,我們將會從Android資源寒砖,本地文件赐劣,一個Uri加載一張圖片。
轉(zhuǎn)載請注明出處:http://weiyf.cn/2016/10/31/Glide-—-Getting-Started/