作者: weiyf
時(shí)間: 2016-10-31 12:32:26
原文鏈接:https://futurestud.io/tutorials/glide-advanced-loading
上周,我們已經(jīng)看了使用Glide的原因以及一個(gè)從網(wǎng)絡(luò)資源加載一張圖片的簡(jiǎn)單例子。但這不僅僅是Glide的唯一加載來(lái)源。Glide還可以從Android資源,文件和Uri來(lái)加載圖片。在這篇博客中,我們會(huì)涵蓋這三種方式。
Last week, we've looked at reasons for using Glide and a simple example request to load an image from an Internet source. But this is not the only possible image source for Glide. Glide can also load images from the Android resources, files and Uri's. In this blog post, we'll cover all three options.
Glide系列提綱概況(Glide Series Overview):
- 【雙語(yǔ)】Glide — 入門(mén)(Glide — Getting Started)
- 【雙語(yǔ)】Glide — 高級(jí)加載(Glide — Advanced Loading)
- 【雙語(yǔ)】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: Customize Caching
- Glide Module Example: Optimizing By Loading Images In Custom Sizes
- Glide — Dynamically Use Model Loaders
- Glide — How to Rotate Images
- Glide — Series Roundup
從資源中加載(Loading from Resources)
首先是從Android資源加載。你使用資源idint
來(lái)替換之前的一個(gè)指向網(wǎng)絡(luò)URL的字符串。
First up is loading from Android resources. Instead of giving a String pointing to an Internet URL, you give a resource int.
int resourceId = R.mipmap.ic_launcher;
Glide
.with(context)
.load(resourceId)
.into(imageViewResource);
如果你對(duì)R.mipmap.感到疑惑,這是Android來(lái)處理icon的新方式
If you're confused by the R.mipmap., it's Android's new way of handling icons.
當(dāng)然,你可以直接使用ImageView
類(lèi)里面的方法來(lái)設(shè)置一個(gè)資源。然而,如果你使用更高級(jí)的話(huà)題例如動(dòng)態(tài)轉(zhuǎn)換,這可能能更有趣。
Of course, you can set a resource directly by using the methods of the ImageView class. However, this can be interesting if you're using more advanced topics like dynamic transformations.
從文件中加載(Loading from File)
其實(shí)是從文件中加載。當(dāng)你讓用戶(hù)選擇一個(gè)照片來(lái)顯示圖片(類(lèi)似于相冊(cè))會(huì)很有用。參數(shù)只是一個(gè)File
對(duì)象。我們來(lái)看一個(gè)例子:
Second up is loading from a file. This can be useful when you let the user select a photo to display an image (similar to a gallery). The parameter is just a File object. In an example, we look:
// this file probably does not exist on your device. However, you can use any file path, which points to an image file
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Running.jpg");
Glide
.with(context)
.load(file)
.into(imageViewFile);
從Uri加載(Loading from Uri)
最后,你可以制定一個(gè)Uri來(lái)加載圖片。這個(gè)請(qǐng)求和之前的沒(méi)什么不同:
Lastly, you can also load images defined by an Uri. The request is no different from the previous options:
// this could be any Uri. for demonstration purposes we're just creating an Uri pointing to a launcher icon
Uri uri = resourceIdToUri(context, R.mipmap.future_studio_launcher);
Glide
.with(context)
.load(uri)
.into(imageViewUri);
一個(gè)簡(jiǎn)單的從資源id轉(zhuǎn)換成Uri
的小工具函數(shù)。
The small helper function is a simple conversion from the resourceId to an Uri.
public static final String ANDROID_RESOURCE = "android.resource://";
public static final String FOREWARD_SLASH = "/";
private static Uri resourceIdToUri(Context context, int resourceId) {
return Uri.parse(ANDROID_RESOURCE + context.getPackageName() + FOREWARD_SLASH + resourceId);
}
然而,這個(gè)Uri不一定是從資源id生成的,可以是任何的Uri。
However, the Uri does not have to be generated from a resourceId. It can be any Uri.
展望(Outlook)
基礎(chǔ)加載原則已經(jīng)完成,現(xiàn)在我們終于可以看到更多有趣的東西。下周我們會(huì)涵蓋在ListView
和GridView
的適配器使用和Glide的緩存。
The basic loading principles are done, now we can finally look at more interesting stuff. Next week we'll cover adapter use and Glide's caching in ListViews and GridViews.
轉(zhuǎn)載請(qǐng)注明出處:http://weiyf.cn/2016/10/31/Glide-—-Advanced-Loading/