作者: 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/
网友评论