自定义双缓存加载网络图片虽然可以实现节省流量,加快加载速等功能,但比较烦所,现有第三方提供的ImageLoader框架,可以简单实现网络加载,双缓存图片功能
1、ImageLoaderConfiguration对象,实现ImageLoader的公共功能设计,只要运行一次,实现整个APP的imageLoader对象都通用这个功能,在这里就运行application的属性,实现了运行一次,全局通用
public class ImageLoaderApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
initImageLoader(getApplicationContext());
}
public static void initImageLoader(Context context) {
//缓存文件的目录
File cacheDir = StorageUtils.getOwnCacheDirectory(context, "imageloader/Cache");
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.memoryCacheExtraOptions(480, 800) // max width, max height,即保存的每个缓存文件的最大长宽
.threadPoolSize(3) //线程池内加载的数量
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.diskCacheFileNameGenerator(new Md5FileNameGenerator()) //将保存的时候的URI名称用MD5 加密
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You can pass your own memory cache implementation/你可以通过自己的内存缓存实现
.memoryCacheSize(2 * 1024 * 1024) // 内存缓存的最大值
.diskCacheSize(50 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值
.tasksProcessingOrder(QueueProcessingType.LIFO)
// 由原先的discCache -> diskCache
.diskCache(new UnlimitedDiskCache(cacheDir))//自定义缓存路径
.imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
.writeDebugLogs() // Remove for release app
.build();
//全局初始化此配置
ImageLoader.getInstance().init(config);
}
}
一定要实向init方法,要不会出错,而且运用了application,要在manifest文件中的<application中加入 android:name="com.example.xin_utils_downwebimage2cachedemo.ImageLoaderApplication">
2、在调用的activity中,声明DisplayImageOptions的对象,这是ImageLoader的个人属性配置:PS有一个图片圆角设置,这里没做,因为做了后显示不了图片,不知道为什么
options属性配置好后,用ImageLoader.getInstance()获取到单例模式ImageLoader的对象
通过imageLoader.displayImage(imageUrls, iv, options, new AnimateFirstDisplayListener());,实现了图片的加载,并显示在相应的iv上,里面也配置了属性options,并在最后实现了监听,在监听中,可以停制加载图片等功能
public class ImageLoaderActivityDemo extends Activity {
ImageLoader imageLoader;
DisplayImageOptions options; // DisplayImageOptions是用于设置图片显示的类
String imageUrls = "http://pic18.nipic.com/20120130/7193741_145433542121_2.jpg"; // 图片路径
ImageView iv;
MyHandler myHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageloader_main);
myHandler = new MyHandler();
iv = (ImageView) findViewById(R.id.imageView1);
imageLoader = ImageLoader.getInstance();
// 使用DisplayImageOptions.Builder()创建DisplayImageOptions
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.loading) // 设置图片下载期间显示的图片
.showImageForEmptyUri(R.drawable.empty) // 设置图片Uri为空或是错误的时候显示的图片
.showImageOnFail(R.drawable.error) // 设置图片加载或解码过程中发生错误显示的图片
.cacheInMemory(true) // 设置下载的图片是否缓存在内存中
.cacheOnDisk(true) // 设置下载的图片是否缓存在SD卡中
.build(); // 创建配置过得DisplayImageOption对象
}
public void addLocalImage(View view) {
System.out.println("加载本地图片lc");
File cacheDir = StorageUtils.getOwnCacheDirectory(this,
"imageloader/Cache");
System.out.println("cacheDir=" + cacheDir);
myHandler.obtainMessage(1).sendToTarget();
}
public void clickDown(View view) {
System.out.println("onclick");
myHandler.obtainMessage(0).sendToTarget();
}
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
// 测试用
imageLoader.displayImage(imageUrls, iv);
break;
case 1:
// 测试用
//imageLoader.displayImage(imageUrls, iv, options);
imageLoader.displayImage(imageUrls, iv, options,
new AnimateFirstDisplayListener());
break;
default:
break;
}
}
}
/**
* 图片加载第一次显示监听器
*
* @author Administrator
*
*/
private static class AnimateFirstDisplayListener extends
SimpleImageLoadingListener {
static final List<String> displayedImages = Collections
.synchronizedList(new LinkedList<String>());
@Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
// 是否第一次显示
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
// 图片淡入效果
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}