在app中处理图片的网络下载,三级缓存可以使用谷歌的开源框架,下载jar包:universal-image-loader-1.9.3.jar,导入项目中。
在Application中配置参数:
public class MyApplication extends Application {
public static Context context;
@Override
public void onCreate() {
super.onCreate();
InitImageLoader(getApplicationContext());
}
private void InitImageLoader(Context ct) {
//设置sd卡缓存的路径
File cacheDir = StorageUtils.getOwnCacheDirectory(ct,"imageloader/Cache");
//图片的参数配置
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)
.showImageOnFail(R.drawable.ic_launcher)
.cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
.cacheOnDisk(true)
.cacheInMemory(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
@SuppressWarnings("deprecation")
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
//图片的最大宽高
//.memoryCacheExtraOptions(480,800)
//线程池中线程的数量
.threadPoolSize(3)
.denyCacheImageMultipleSizesInMemory()
.defaultDisplayImageOptions(defaultOptions)
.discCache(new UnlimitedDiscCache(cacheDir))
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO).build();
ImageLoader.getInstance().init(config);
}
}
在调用的地方直接写:
ImageLoader.getInstance().displayImage(ImageUrl,ImageView);就可以了
displayImage()这个方法有很多不同的参数,可以选择自己需要的。