import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.ref.SoftReference; import java.util.HashMap; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Handler; import android.os.Message; /** * @Author Tal * @Time 2011-3-16上午11:27:16 * @Email dangzhengtao@gamil.com * @Type 异步加载图片 */ public class AsyncImageLoader { /** * SoftReference是软引用,是为了更好的为了系统回收变量,重复的URL直接返回已有的资源,实现回调函数,让数据成功后,更新到UI线程。 * 在Java中内存管理,引用分为四大类,强引用HardReference、弱引用WeakReference、软引用SoftReference和虚引用PhantomReference。 * 它们的区别也很明显,HardReference对象是即使虚拟机内存吃紧抛出OOM也不会导致这一引用的对象被回收, * 而WeakReference等更适合于一些数量不多,但体积稍微庞大的对象,在这四个引用中,它是最容易被垃圾回收的, * 而我们对于显示类似Android Market中每个应用的App Icon时可以考虑使用SoftReference来解决内存不至于快速回收, * 同时当内存短缺面临Java VM崩溃抛出OOM前时,软引用将会强制回收内存,最后的虚引用一般没有实际意义, * 仅仅观察GC的活动状态,对于测试比较实用同时必须和ReferenceQueue一起使用。 */ private HashMap> imageCache = null; public AsyncImageLoader() { imageCache = new HashMap>(); } public Bitmap loadBitmap(final String imageUrl, final ImageCallback imageCallback) { if (imageCache.containsKey(imageUrl)) { SoftReference softReference = imageCache.get(imageUrl); Bitmap Bitmap = softReference.get(); if (Bitmap != null) { return Bitmap; } } final Handler handler = new Handler() { public void handleMessage(Message message) { imageCallback.imageLoaded((Bitmap) message.obj, imageUrl); } }; new Thread() { @Override public void run() { Bitmap bitmap = loadImageFromUrl(imageUrl); if(bitmap!=null){ imageCache.put(imageUrl, new SoftReference(bitmap)); Message message = handler.obtainMessage(0, bitmap); handler.sendMessage(message); } } }.start(); return null; } public static Bitmap loadImageFromUrl(String path) { try { InputStream is = NetWorkUtil.getInputStreamFromUrl(path); if(is != null){ Bitmap bm = BitmapFactory.decodeStream(new FlushedInputStream(is)); return bm; }else{ return null; } } catch (Exception e) { e.getStackTrace(); System.out.println("asy is error :"+e.getMessage()); return null; } } static class FlushedInputStream extends FilterInputStream { public FlushedInputStream(InputStream inputStream) { super(inputStream); } @Override public long skip(long n) throws IOException { long totalBytesSkipped = 0L; while (totalBytesSkipped < n) { long bytesSkipped = in.skip(n - totalBytesSkipped); if (bytesSkipped == 0L) { int b = read(); if (b < 0) { break; // we reached EOF } else { bytesSkipped = 1; // we read one byte } } totalBytesSkipped += bytesSkipped; } return totalBytesSkipped; } } public interface ImageCallback { public void imageLoaded(Bitmap imageBitmap, String imageUrl); } } -************************************************************************** 在adapter里面使用: final ImageView image = viewCache.image; image.setTag(url); Bitmap cachedImage = asyncImageLoader.loadBitmap(url, new ImageCallback() { public void imageLoaded(Bitmap imageDrawable, String imageUrl) { image.setImageBitmap(imageDrawable); } }); if (cachedImage == null) { image.setImageResource(R.drawable.icon); }else{ //image.setImageDrawable(cachedImage); image.setImageBitmap(cachedImage); }

更多相关文章

  1. Android中的Context对象
  2. android 自定义线程池ThreadPoolUtils工具类
  3. Android为每个应用程序分配的内存大小是多
  4. Android进程与线程基本知识四
  5. android利用Handler开启线程和关闭线程
  6. android拍照造成内存泄露问题
  7. Android开发学习笔记(十二) 获取系统可用内存
  8. Android中图片占用内存的计算
  9. 在Android中使用Handler和Thread线程执行后台操作

随机推荐

  1. [置顶] 进击的Android注入术《三》
  2. 《Android移动应用基础教程》(Android Stu
  3. Android 开发之 ---- 底层驱动开发(一)
  4. 探索Android软键盘的疑难杂症
  5. Android(安卓)App组件之Fragment说明和示
  6. Android(安卓)Java包
  7. [5] Zygote
  8. Android 使用shape来优化界面效果
  9. 【Android NDK 开发】Ubuntu 函数库交叉
  10. mac上的android apk安装器