android图片异步加载解决oom问题方法,github上的开源项目。相当好用的一套图片加载的方法,外国人就是牛叉。。。
我用的是universal-image-loader-1.8.6-with-sources.jar这个版本的。
1,在application中初始化imageLoader,
File cacheDir = null;
if(FileSaveName.isHaveSDcard()){//sd存在的时候保存到sd卡
cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), FileSaveName.SaveImageloderPaht);
StorageUtils.getCacheDirectory(applicationContext);
}else{
cacheDir = StorageUtils.getIndividualCacheDirectory(applicationContext);
}
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(applicationContext)
.threadPriority(Thread.NORM_PRIORITY - 2)
.memoryCacheExtraOptions(720, 1280)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.discCache(new UnlimitedDiscCache(cacheDir))
.tasksProcessingOrder(QueueProcessingType.LIFO)
.build();
ImageLoader.getInstance().init(config);
2,使用的时候,先设置。
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.default_small_pic)
.showImageForEmptyUri(R.drawable.default_small_pic)
.showImageOnFail(R.drawable.default_small_pic)
.cacheOnDisc(true)//缓存到sd卡
.cacheInMemory(true)//加入内存中,listview滑动的时候不会出现闪动情况
.preProcessor(new BitmapProcessor() {//这段代码不一定要,有自动缩放的方法,我这里是要列表图都缩放到一样大。
@Override
public Bitmap process(Bitmap bm) {
// TODO Auto-generated method stub
int width = bm.getWidth();
int height = bm.getHeight();
//计算缩放比例
float scaleWidth = ((float) 80) / width;
float scaleHeight = ((float) 65) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width,
height, matrix, true);
return newbm;
}
})
.imageScaleType(ImageScaleType.NONE)//缩放方式
.build();
3,开始正式使用
imageLoader.displayImage(actile.getImage(), holder.imageView,options);
4,里面还有一些其他的方法,需要慢慢摸索。