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(安卓)内存管理 &Memory Leak & OOM 分析
  2. [置顶] Android防止内存溢出浅析
  3. 且谈Android内存溢出
  4. Android内存管理的原理--进程管理
  5. android 算定义布局xml
  6. android自带Base64加密解密
  7. android自带Base64加密解密
  8. 动态设置 view 宽高
  9. Android(安卓)Module中导入aar

随机推荐

  1. Android框架搭建(MVP + Dagger2 + Retrof
  2. android中圆角图像生成方法
  3. android在一个程序中启动另一个程序
  4. Qt 5.7.0 编译Android(安卓)纯C++ 程序
  5. android-image-slide-panel图片照片墙的
  6. Android消除Toast延迟显示
  7. Android(安卓)-- 倒计时Button的几种实现
  8. Android中AIDL的使用
  9. Android(安卓)wifi打开流程(Android(安卓
  10. 如何在eclipse中添加android ADT