ImageLoader是一个加载图片的开源框架,其基本功能为加载本地和网络图片。
github地址:https://github.com/nostra13/Android-Universal-Image-Loader
首先看一个封装好的工具方法:
该工具方法中包含了几乎ImageLoader的常用方法,包括初始化、网络图片的加载、图片处理(圆形、圆角)、清除缓存等等
public class MyImageLoader {
private static final int MEMORY_CACHE_SIZE = 2 * 1024;
private static final int DISK_CACHE_SIZE = 50 * 1024 * 1024;
/**
* 加载网络图片,不处理
*/
private static final DisplayImageOptions OPTIONS_NORMAL = new DisplayImageOptions.Builder()
.cacheOnDisk(true).cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.considerExifParams(true)
.build();
/**
* 加载本地图片,不缓存,不存储
*/
private static final DisplayImageOptions PHOTO_SELECT = new DisplayImageOptions.Builder()
.cacheOnDisk(false).cacheInMemory(false)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.considerExifParams(true)
.build();
/**
* 加载圆形图片,圆形边框为白色,边框宽度为5
*/
private static final DisplayImageOptions OPTIONS_CIRCLE = new DisplayImageOptions.Builder()
.cacheOnDisk(true).cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new CircleBitmapDisplayer(Color.WHITE, 5))
.considerExifParams(true)
.build();
/**
* 加载圆角图片,圆角弧度为8
*/
private static final DisplayImageOptions OPTIONS_ROUND_RADIUS = new DisplayImageOptions.Builder()
.cacheOnDisk(true).cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new RoundedBitmapDisplayer(8))
.considerExifParams(true)
.build();
private static ImageLoader instance;
/**
* 获取缓存的图片路径
*
* @return
*/
public static ImageLoader getImageLoader() {
if (instance == null) {
init(MyApplication.GetApplication());
}
return instance;
}
private static void init(Context appContext) {
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
appContext)
.threadPoolSize(3)
.threadPriority(Thread.NORM_PRIORITY - 2)
.tasksProcessingOrder(QueueProcessingType.LIFO)
.denyCacheImageMultipleSizesInMemory()
.imageDownloader(
new BaseImageDownloader(appContext, 10000, 10000))
.diskCacheFileNameGenerator(new Md5FileNameGenerator())
.diskCacheSize(DISK_CACHE_SIZE)
.memoryCache(new WeakMemoryCache())
.memoryCacheSize(MEMORY_CACHE_SIZE).build();
ImageLoader.getInstance().init(config);
instance = ImageLoader.getInstance();
}
public static void display(String url, ImageView imageView,
DisplayImageOptions options) {
if (instance == null) {
init(MyApplication.GetApplication());
}
instance.displayImage(url, imageView, options);
}
/**
* 获取普通不处理图片
*
* @param url
* @param imageView
*/
public static void displayNormal(String url, ImageView imageView) {
display(url, imageView, OPTIONS_NORMAL);
}
/**
* 获取相册照片
*
* @param url
* @param imageView
*/
public static void displayPhoto(String url, ImageView imageView) {
display(url, imageView, PHOTO_SELECT);
}
/**
* 获取圆形图片
*
* @param url
* @param imageView
*/
public static void displayCircle(String url, ImageView imageView) {
display(url, imageView, OPTIONS_CIRCLE);
}
/**
* 获取圆角图片
*
* @param url
* @param imageView
*/
public static void displayRoundRadius(String url, ImageView imageView) {
display(url, imageView, OPTIONS_ROUND_RADIUS);
}
/**
* 清除缓存
*/
public static void clearCache() {
if (instance != null) {
instance.clearMemoryCache();
instance.clearDiskCache();
}
}
/**
* 获取url缓存的本地图片路径
*
* @return
*/
public static File getImageLoaderFile(String url) {
if (instance == null) {
init(MyApplication.GetApplication());
}
File file = instance.getDiskCache().get(url);
return file;
}
}
1、网络和本地图片的加载;
2、将加载的图片处理成圆形或者圆角
3、清除内存中的缓存
4、清除SD卡中的缓存
5、获取相应url的缓存路径
用法补充:
1、可以加载的图片的路径的写法:
"http://site.com/image.png" // from Web "file:///mnt/sdcard/image.png" // from SD card "file:///mnt/sdcard/video.mp4" // from SD card (video thumbnail) "content://media/external/images/media/13" // from content provider "content://media/external/video/media/13" // from content provider (video thumbnail) "assets://image.png" // from assets "drawable://" + R.drawable.img // from drawables (non-9patch images)2、获取Bitmap
ImageLoader imageLoader = ImageLoader.getInstance(); // Get singleton instance
// Load image, decode it to Bitmap and return Bitmap synchronously
Bitmap bmp = imageLoader.loadImageSync(imageUri);
imageLoader.displayImage(imageUri, imageView, options, new ImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { ... } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { ... } @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { ... } @Override public void onLoadingCancelled(String imageUri, View view) { ... } }, new ImageLoadingProgressListener() { @Override public void onProgressUpdate(String imageUri, View view, int current, int total) { ... } });4、指定图片大小
// Load image, decode it to Bitmap and return Bitmap to callback ImageSize targetSize = new ImageSize(80, 50); // result Bitmap will be fit to this size imageLoader.loadImage(imageUri, targetSize, options, new SimpleImageLoadingListener() { @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { // Do whatever you want with Bitmap } });5、其他的一些方法可以通过类库中的代码获取,比如通过DiskCache可以知道通过url调用remove方法直接删除缓存在SD卡的图片,如图
6、若想要定制更多的图片样式可以通过继承BitmapDisplayer做到,类库中本身也是这么做的,如图是类库自己定制的圆形图片的类,通过重写其中的方法即可实现
7、通过可定制的方式,可以使用第三方的网络请求框架,比如github中sample中给出的使用Okhttp的网络请求,代码如下:
public class OkHttpImageDownloader extends BaseImageDownloader {
private OkHttpClient client;
public OkHttpImageDownloader(Context context, OkHttpClient client) {
super(context);
this.client = client;
}
@Override
protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
Request request = new Request.Builder().url(imageUri).build();
ResponseBody responseBody = client.newCall(request).execute().body();
InputStream inputStream = responseBody.byteStream();
int contentLength = (int) responseBody.contentLength();
return new ContentLengthInputStream(inputStream, contentLength);
}
}