这个可以实现ImageView异步加载图片,内存缓存,文件缓存,imageview显示图片时增加淡入淡出动画。
解决了:
1. listview加载oom问题
2. listview加载时卡顿的现象
3. listview加载时item中图片重复错位等情况
可以配置:
1. 设置加载图片的最大尺寸
2. 设置默认图片的显示
3. 设置图片位图模式
4. 设置内存缓存的最大值。
5. 文件缓存保存的目录
这个框架基本的代码是很久以前不知道哪里弄的,零零碎碎的,现在已经优化了很多,所以现在上传到github上共享。
讲讲使用方式吧:
首先使用前下载源码或者jar包(见github:https://github.com/wangjiegulu/ImageLoaderSample)
然后进行图片加载器(ImageLoader)的配置和初始化,推荐的方法如下:
新建MyApplication类,继承Application,在onCreate中增加如下代码:
复制代码
1 /**
2* Created with IntelliJ IDEA.
3* Author: wangjieemail:tiantian.china.2@gmail.com
4* Date: 14-2-27
5* Time: 上午11:25
6*/
7 public class MyApplication extends Application{
8@Override
9public void onCreate() {
10super.onCreate();
11ImageLoader.init(getApplicationContext(),
12new CacheConfig()
13.setDefRequiredSize(600) // 设置默认的加载图片尺寸(表示宽高任一不超过该值,默认是70px)
14.setDefaultResId(R.drawable.ic_launcher) // 设置显示的默认图片(默认是0,即空白图片)
15.setBitmapConfig(Bitmap.Config.ARGB_8888) // 设置图片位图模式(默认是Bitmap.CacheConfig.ARGB_8888)
16.setMemoryCachelimit(Runtime.getRuntime().maxMemory() / 3) // 设置图片内存缓存大小(默认是Runtime.getRuntime().maxMemory() / 4)
17 //.setFileCachePath(Environment.getExternalStorageDirectory().toString() + "/mycache") // 设置文件缓存保存目录
18);
19
20}
21
22
23……
24 }
复制代码
然后再AndroidManifest.xml中添加:
<application
......
android:name="MyApplication">
......
</application>
到此,配置已经全部完成:
接下来,使用ImageLoader来加载图片:
复制代码
1 holder.progress.setText("0%");
2holder.progress.setVisibility(View.VISIBLE);
3final ViewHolder vhr = holder;
4ImageLoader.getInstances().displayImage(list.get(position), holder.image, new ImageLoader.OnImageLoaderListener() {
5@Override
6public void onProgressImageLoader(ImageView imageView, int currentSize, int totalSize) {
7vhr.progress.setText(currentSize * 100 / totalSize + "%");
8}
9
10@Override
11public void onFinishedImageLoader(ImageView imageView, Bitmap bitmap) {
12vhr.progress.setVisibility(View.GONE);
13}
14});
15或者:
16ImageLoader.getInstances().displayImage(url, imageIv);
17或者
18ImageLoader.getInstances().displayImage(url, imageIv, 100);