尊重他人劳动成果,转载请说明出处:http://blog.csdn.net/bingospunky/article/details/44344085

接触android有半年了。关于图片异步载入。一直仅仅用别人的框架,尽管特别方便,可是始终未见识到图片异步载入的庐山真面目。

近期比較悠闲,研究一些高大上的东西。在这篇文章总结一下我对图片异步载入的一些学习心得。

图片载入最重要的无非就是内存和线程。

大家都知道关于内存溢出一般的解决方式就是LruCache,在我的这个demo里我仅仅要使用SoftReference实现了一个缓存类,SoftReference已经被遗弃了,这里仅仅为学习。

代码

com.example.qtdemo_oom.Cache<T, U>

/**
* 由SoftReference实现的内存缓存<br>
* 此类仅供学习,因为Android 2.3開始,垃圾回收器倾向于回收软引用和弱引用对象。
* @author qingtian
*/
public class Cache<T,U> { private Hashtable<T, NewSoftReference> hashTable;// 用于Chche内容的存储
private ReferenceQueue<U> q;// 垃圾Reference的队列 /**
* 继承SoftReference,使得每个实例都具有可识别的标识。 */
private class NewSoftReference extends SoftReference<U> {
public T key;
public NewSoftReference(U em, ReferenceQueue<U> q) {
super(em, q);
}
} /**
* 构建一个缓存器实例
*/
public Cache() {
hashTable = new Hashtable<T, NewSoftReference>();
q = new ReferenceQueue<U>();
} /**
* 保存一个实例
* @param t
* @param u
*/
public void put(T t, U u) {
cleanCache();// 清除垃圾引用
NewSoftReference ref = new NewSoftReference(u, q);
ref.key = t;
hashTable.put(t, ref);
} /**
* 通过uri获取实例
* @param t
* @return
*/
public U get(T t) {
U bm = null;
if (hashTable.containsKey(t)) {
NewSoftReference ref = (NewSoftReference) hashTable.get(t);
bm = (U) ref.get();
}
return bm;
} /**
* 在hashTable里里清除已经被回收了的对象的软引用对象
*/
@SuppressWarnings("unchecked")
private void cleanCache() {
NewSoftReference ref = null;
while ((ref = (NewSoftReference) q.poll()) != null) {
hashTable.remove(ref.key);
}
} /**
* 清除Cache内的所有内容
*/
public void clearCache() {
cleanCache();
hashTable.clear();
System.gc();
System.runFinalization();
}
}

com.example.qtdemo_oom.ImageLoader

/**
* 图片载入工具类
* @author qingtian
*/
public class ImageLoader { private static final String TAG = "qingtian" ; //线程池
ExecutorService threadPool ;
//自己实现的缓存类
Cache<String, Bitmap> cache;
//也能够用v4包提供的缓存
// LruCache<String, Bitmap> cache; public ImageLoader() {
threadPool = new ThreadPoolExecutor(2, 2, 0, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());
cache = new Cache<String, Bitmap>();
// int maxMemory = (int) Runtime.getRuntime().maxMemory();
// cache = new LruCache<String, Bitmap>(maxMemory/8){
// // 測量Bitmap的大小
// @Override
// protected int sizeOf(String key, Bitmap value) {
// return value.getRowBytes() * value.getHeight();
// }
// };
} private int currentThreadNum = 0; class DownBitmapRunnable implements Runnable{
ImageView iv; String uri;
public DownBitmapRunnable( ImageView iv, String uri) {
this.iv = iv;
this.uri = uri;
}
@Override
public void run() { currentThreadNum++; show(); Bitmap bm = cache.get(uri);
if (bm == null) {
Log.i(TAG, "下载");
bm = BitmapUtil.getImageBitmap(uri);
}else{
Log.i(TAG, "不下载");
}
if (bm != null) {
cache.put(uri, bm);
final Bitmap fBitmap = bm;
new Handler(Looper.getMainLooper()).post(new Runnable() { @Override
public void run() {
iv.setImageBitmap(fBitmap);
} });
} currentThreadNum--;
} } public void show( ImageView iv, String uri) { threadPool.execute(new DownBitmapRunnable(iv, uri)); } public void show() {
Log.i(TAG, "currentThreadNum:" + currentThreadNum );
}
}

在这个类里。使用线程池管理线程。避免创建/取消线程时内存的浪费。第14行代码也提供了LruCache代码,我们也能够用LruCache来实现缓存。

代码第54--61行,对于view的操作已经在主线程里了。

com.example.qtdemo_oom.Adapter

public class Adapter extends BaseAdapter {
public static final String[] uris = new String[] {
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img1b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img2b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img3b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img4b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img5b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img6b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img7b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img8b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img9b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img10b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img11b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img12b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img13b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img14b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img15b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img16b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img17b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img18b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img19b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img20b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img21b.jpg",
"http://mabinbin.com:8080/AndroidAsyncHttpService/image/img22b.jpg" }; public LayoutInflater mInflater;
public ImageLoader loader; public Adapter(Context context) {
mInflater = LayoutInflater.from(context);
loader = new ImageLoader();
} @Override
public int getCount() {
return uris.length;
} @Override
public Object getItem(int arg0) {
return uris[arg0];
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item, null); holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.tv);
holder.iv = (ImageView) convertView.findViewById(R.id.iv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
} holder.tv.setText((String) getItem(position)); loader.show(holder.iv, uris[position]); return convertView;
} class ViewHolder {
TextView tv;
ImageView iv;
} }

代码第66行,就能够异步载入图片了。

com.example.qtdemo_oom.util.BitmapUtil

public class BitmapUtil {

	public static Bitmap getImageBitmap(String uri) {
URL imgUrl = null;
Bitmap bitmap = null;
InputStream is = null;
try {
imgUrl = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) imgUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
is = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bitmap;
} }

通过这个简单的demo,我见到了内存缓存的真面目。也学习了LruCache的源代码(这里没体现)。

当然这是我学习异步图片载入的第一版,希望在后面的版本号里写的更成熟。

源代码下载

自己动手写android图片异步载入库的更多相关文章

  1. Android图片异步加载

    原:http://www.cnblogs.com/angeldevil/archive/2012/09/16/2687174.html 相关:https://github.com/nostra13/A ...

  2. 一起写一个Android图片加载框架

    本文会从内部原理到具体实现来详细介绍如何开发一个简洁而实用的Android图片加载缓存框架,并在内存占用与加载图片所需时间这两个方面与主流图片加载框架之一Universal Image Loader做 ...

  3. Android 图片异步加载的体会,SoftReference已经不再适用

      在网络上搜索Android图片异步加载的相关文章,目前大部分提到的解决方案,都是采用Map<String, SoftReference<Drawable>>  这样软引用的 ...

  4. Android图片异步加载之Android-Universal-Image-Loader

    将近一个月没有更新博客了,由于这段时间以来准备毕业论文等各种事务缠身,一直没有时间和精力沉下来继续学习和整理一些东西.最近刚刚恢复到正轨,正好这两天看了下Android上关于图片异步加载的开源项目,就 ...

  5. Android图片异步加载之Android-Universal-Image-Loader(转)

    今天要介绍的是Github上一个使用非常广泛的图片异步加载库Android-Universal-Image-Loader,该项目的功能十分强大,可以说是我见过的目前功能最全.性能最优的图片异步加载解决 ...

  6. Android图片加载框架最全解析(六),探究Glide的自定义模块功能

    不知不觉中,我们的Glide系列教程已经到了第六篇了,距离第一篇Glide的基本用法发布已经过去了半年的时间.在这半年中,我们通过用法讲解和源码分析配合学习的方式,将Glide的方方面面都研究了个遍, ...

  7. fackbook的Fresco (FaceBook推出的Android图片加载库-Fresco)

    [Android开发经验]FaceBook推出的Android图片加载库-Fresco   欢迎关注ndroid-tech-frontier开源项目,定期翻译国外Android优质的技术.开源库.软件 ...

  8. Android 图片加载库Glide 实战(二),占位符,缓存,转换自签名高级实战

    http://blog.csdn.net/sk719887916/article/details/40073747 请尊重原创 : skay <Android 图片加载库Glide 实战(一), ...

  9. Android图片加载框架最全解析(八),带你全面了解Glide 4的用法

    本篇将是我们这个Glide系列的最后一篇文章. 其实在写这个系列第一篇文章的时候,Glide就推出4.0.0的RC版了.那个时候因为我一直研究的都是Glide 3.7.0版本,再加上RC版本还不太稳定 ...

随机推荐

  1. Java并发(1)- 聊聊Java内存模型

    引言 在计算机系统的发展过程中,由于CPU的运算速度和计算机存储速度之间巨大的差距.为了解决CPU的运算速度和计算机存储速度之间巨大的差距,设计人员在CPU和计算机存储之间加入了高速缓存来做为他们之间 ...

  2. gvim设置使用

    最近有一款编辑器叫sublimeText 2比较流行,我也下载用了一下,确实很好看,自动完成,缩进功能什么的也比较齐全,插件也十分丰富.但用起来不是很顺手,最后还是回到了Gvim(Vim的GUI版本, ...

  3. 简单粗暴!解决锐捷强制关闭VMware NAT Service的问题(图文教程)

    众所周知毒瘤的锐捷会定时强制关闭NAT服务,导致虚拟机连不上网,进而你的虚拟机就成了孤儿,只能玩单机. 在一番百度后,得到了一种神仙破解办法.原理是把锐捷关闭服务时所用的搜索关键字改掉,这样锐捷就搜不 ...

  4. 【IDEA】IDEA下maven项目无法提示和使用EL表达式的解决办法

    今天在IDEA创建web项目之后发现无法使用EL和JSTL, 一.如果JSP中无法自动提示EL表达式,比如${pageContext.request.contextPath},可在pom.xml的&l ...

  5. 转:python安装pycrypto

    from: http://ljhzzyx.blog.163.com/blog/static/3838031220136592824697/   在windows下用一下开源工具就是悲催,如题pytho ...

  6. Linux内存管理图解【转】

    转自:http://www.360doc.com/content/13/0505/15/12218157_283128759.shtml Linux内存管理图解 2013-05-05  果儿的百科   ...

  7. 基于UDP高性能传输协议UDT doc翻译(一)

    原文转自:http://hi.baidu.com/doodlezone/item/74a203155efe26dbbf9042dd                  UDT文档阅读理解 一.  概述 ...

  8. HashMap和Hashtable 线程安全性

    HashMap和Hashtable的比较是Java面试中的常见问题,用来考验程序员是否能够正确使用集合类以及是否可以随机应变使用多种思路解决问题.HashMap的工作原理.ArrayList与Vect ...

  9. bzoj 1305: [CQOI2009]dance跳舞

    题目链接 bzoj 1305: [CQOI2009]dance跳舞 题解 男,女生拆点A1A2,B1B2,拆成两点间分别连容量为K的边,限制与不喜欢的人跳舞的数量 A1连接源点容量为x,B1连接汇点容 ...

  10. SQL 连表更新

    现在数据如下: 需求如下: 要求 更新 Earnings 表中的Earnings字段 当 Table_2 KPI 大于等于1时 Earinings = KPI* 2000,否则等于 KPI* 1500 ...