在Android开发中对图片资源的处理已经司空见惯了,如果还是使用Android API提供的方法来处理图片(尤其是网络图片),不仅复杂工作量大,而且还达不到好的一个效果.所以最终选择一些图片处理框架,如Android-Universal-Image-Loader,picasso(square出品,必属精品),Fresco(facebook家的),等等.本文主要就是讲Android-Universal-Image-Loader的工具类的封装,我也封装过picasso,picasso可能会在下一篇博文里介绍吧.其实,掌握了一个图片处理框架就可以掌握其他图片处理框架.
我使用的开发工具是AS,如果你还在使用Eclipse,那么赶紧转到AS吧,AS真的太好用了,真的!
进入正文了:
在你项目app下的build.gradle的dependencies下加入下面这句话就可以将这个框架集成到自己的项目中了.有好几种方法(你也可以去GitHub上看),选了最方便的,这也是使用AS的好处之一吧.
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
好了,可以封装了,直接贴代码了.
package com.bighuan.bighuansdk.imageloader;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
/**
* 项目名: Component
* 包名: com.bighuan.bighuansdk.imageloader
* 文件名: ImageLoaderManager
* 作者: bighuan
* 创建时间:2017/2/28 20:34
* 描述: UniverseImageLoader工具类,加载图片,尤其是网络图片
*/
public class ImageLoaderManager {
private static final int THREAD_COUNT = 4;//表明我们的UIL最多可以有多少条线程
private static final int PRIORITY = 2; //图片加载的一个优先级
private static final int MEMORY_CACHE_SIZE = 2 * 1024 * 1024;//内存缓存大小
private static final int DISK_CACHE_SIZE = 50 * 1024 * 1024;//表明UIL最多缓存多少图片
private static final int CONNECTION_TIME_OUT = 5 * 1000;//连接超时时间
private static final int READ_TIME_OUT = 30 * 1000;//读取超时时间
private static ImageLoaderManager mInstance = null;
private static ImageLoader mLoader = null;
public static ImageLoaderManager getInstance(Context context) {
if (mInstance == null) {
synchronized (ImageLoaderManager.class) {
if (mInstance == null) {
mInstance = new ImageLoaderManager(context);
}
}
}
return mInstance;
}
/**
* 私有构造方法完成初始化工作
*
* @param context
*/
private ImageLoaderManager(Context context) {
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration
.Builder(context)
.threadPoolSize(THREAD_COUNT)//配置图片线程下载的最大数量
.threadPriority(Thread.NORM_PRIORITY - PRIORITY)//设置优先级
.denyCacheImageMultipleSizesInMemory()//防止缓存多套图片到内存中
.memoryCache(new WeakMemoryCache())//使用弱引用内存缓存
.diskCacheSize(DISK_CACHE_SIZE)//分配硬盘缓存大小
.diskCacheFileNameGenerator(new Md5FileNameGenerator())//使用MD5命名文件
.tasksProcessingOrder(QueueProcessingType.LIFO)//图片下载顺序
.defaultDisplayImageOptions(getDefaultOptions())//默认的图片加载Options
.imageDownloader(new BaseImageDownloader(context, CONNECTION_TIME_OUT, READ_TIME_OUT))
//设置图片下载器
.writeDebugLogs()//debug环境下输出日志
.build();
ImageLoader.getInstance().init(configuration);
mLoader = ImageLoader.getInstance();
}
/**
* 默认的图片显示Options,可设置图片的缓存策略,编解码方式等,非常重要
*
* @return
*/
private DisplayImageOptions getDefaultOptions() {
DisplayImageOptions options = new DisplayImageOptions.Builder()
// .showImageForEmptyUri(R.drawable...)//图片地址为空的时候显示的图片
// .showImageOnFail(R.drawable...)//出错的时候显示的图片
.cacheInMemory(true)//设置图片可以缓存在内存中
.cacheOnDisk(true)//设置图片可以缓存在硬盘中
.considerExifParams(true)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.bitmapConfig(Bitmap.Config.RGB_565)//使用的图片解码类型
.decodingOptions(new BitmapFactory.Options())//图片解码配置
.resetViewBeforeLoading(true)
.build();
return options;
}
/**
* 设置图片
*
* @param imageView
* @param path
* @param listener
* @param options
*/
public void displayImage(ImageView imageView, String path,
ImageLoadingListener listener, DisplayImageOptions options) {
if (mLoader != null) {
mLoader.displayImage(path, imageView, options, listener);
}
}
public void displayImage(ImageView imageView, String path, ImageLoadingListener listener) {
if (mLoader != null) {
mLoader.displayImage(path, imageView, listener);
}
}
public void displayImage(ImageView imageView, String path) {
displayImage(imageView, path, null);
}
}
封装这个框架主要用了单例模式,代码中都有注释,这里就不在解释了,大家肯定也看的懂.通过封装Android-Universal-Image-Loader,我们在实际使用时只要一两行代码就可以实现想要的效果,而不用写一大堆代码了,大大的减少了代码冗余.好了,本文到此结束,希望能对一些人有帮助.