懒加载ImageLoader的应用
来源:互联网 发布:曲线图制作软件 编辑:程序博客网 时间:2024/05/16 05:36
创建一个方法类ImageLoader,构造函数含有三个参数:图片地址 Url、图片对象 ImageVIew、是否只从缓存中获取 fromCache;
private ImageLoader mImageLoader;
mImageLoader.DisplayImage(url, viewHolder.comment_item_img, false);
public class ImageLoader {
private MemoryCache memoryCache = new MemoryCache();
private AbstractFileCache fileCache;
private Map<ImageView, String> imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
// 线程池
private ExecutorService executorService;
public ImageLoader(Context context) {
fileCache = new FileCache(context);
//建立一个固定的线程池的大小
executorService = Executors.newFixedThreadPool(5);
}
// 最主要的方法
public void DisplayImage(String url, ImageView imageView, boolean isLoadOnlyFromCache) {
imageViews.put(imageView, url);
// 先从内存缓存中查找
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null)
imageView.setImageBitmap(bitmap);
else if (!isLoadOnlyFromCache) {
// 若没有的话则开启新线程加载图片
queuePhoto(url, imageView);
}
}
private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
// 先从文件缓存中查找是否有
Bitmap b = null;
if (f != null && f.exists()) {
b = decodeFile(f);
}
if (b != null) {
return b;
}
// 最后从指定的url中下载图片
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex) {
Log.e("", "getBitmap catch Exception...\nmessage = " + ex.getMessage());
return null;
}
}
- 懒加载ImageLoader的应用
- 图片的加载ImageLoader
- imageloader加载的详解
- ImageLoader的简单应用
- ImageLoader的应用
- ImageLoader 的应用
- ImageLoader 的应用
- ImageLoader的简单应用
- imageloader的应用
- ImageLoader的各种加载方式
- ImageLoader加载https的图片
- ImageLoader的加载工具类
- imageLoader加载图片的框架
- Imageloader加载
- ImageLoader加载图片慢的问题
- 使用Imageloader加载Bmob上的图片
- 异步加载图片框架ImageLoader的使用
- 图片加载的框架 Imageloader Picasso Glide
- 线程堆栈
- struts2中的默认值
- iTween:Getting Started!
- jws注解类型
- 全球最快手机传输工具在阿里云上——快牙
- 懒加载ImageLoader的应用
- 怎么解决这个eclipse问题?
- tomcat 调优
- phonegap集成html
- 转行IT网
- 使用HTML5 canvas做地图(3)图片加载平移放大缩小
- JavaScript创世神话之创世篇
- Bundler,PMVS,CMVS的编译与使用
- Java回调机制分析