android 使用异步加载图片

    技术2022-05-20  61

    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<String, SoftReference<Bitmap>> imageCache = null; public AsyncImageLoader() { imageCache = new HashMap<String, SoftReference<Bitmap>>(); } public Bitmap loadBitmap(final String imageUrl, final ImageCallback imageCallback) { if (imageCache.containsKey(imageUrl)) { SoftReference<Bitmap> 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>(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); }


    最新回复(0)