作者: weiyf
時間: 2016-10-31 12:32:26
原文鏈接:https://futurestud.io/tutorials/glide-advanced-loading
上周,我們已經看了使用Glide的原因以及一個從網絡資源加載一張圖片的簡單例子。但這不僅僅是Glide的唯一加載來源。Glide還可以從Android資源,文件和Uri來加載圖片。在這篇博客中,我們會涵蓋這三種方式。
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):
- 【雙語】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: 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
來替換之前的一個指向網絡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);
如果你對R.mipmap.感到疑惑,這是Android來處理icon的新方式
If you're confused by the R.mipmap., it's Android's new way of handling icons.
當然,你可以直接使用ImageView
類里面的方法來設置一個資源。然而,如果你使用更高級的話題例如動態轉換,這可能能更有趣。
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)
其實是從文件中加載。當你讓用戶選擇一個照片來顯示圖片(類似于相冊)會很有用。參數只是一個File
對象。我們來看一個例子:
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)
最后,你可以制定一個Uri來加載圖片。這個請求和之前的沒什么不同:
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);
一個簡單的從資源id轉換成Uri
的小工具函數。
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);
}
然而,這個Uri不一定是從資源id生成的,可以是任何的Uri。
However, the Uri does not have to be generated from a resourceId. It can be any Uri.
展望(Outlook)
基礎加載原則已經完成,現在我們終于可以看到更多有趣的東西。下周我們會涵蓋在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.
轉載請注明出處:http://weiyf.cn/2016/10/31/Glide-—-Advanced-Loading/