Android实现图片异步加载操作

本文介绍了一种在Android应用中实现图片异步加载的方法,通过使用软引用缓存已加载的图片,避免了重复下载,并确保了应用不会因图片加载而阻塞UI线程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Android开发过程中,为了防止阻塞UI,图片加载时经常采用异步的方法来加载,异步加载图片的主要流程是进行判断缓存中是否存在图片,如果存在则直接返回,如果不存在则进行下载并进行缓存。


以下是建立一个异步下载类:

  1. /** 
  2. * User: Tom 
  3. * Date: 13-5-13 
  4. * Time: 下午8:07 
  5. */ 
  6. public class AsnycImageLoader { 
  7.  
  8. //定义一个HashMap进行存放缓存的Image key为String Value为一个弱引用的一个资源文件 
  9. // 图片 为了方便JAVA的回收 
  10. private Map<String, SoftReference<Drawable>> imageCache = null
  11. public AsnycImageLoader() { 
  12. imageCache = new HashMap<String, SoftReference<Drawable>>(); 
  13.  
  14. /** 
  15. * 加载图片 
  16. * <p>imageurl为下载资源的URL, 
  17. * ImageCallback当缓存中不存在相关图片时时行网络下载 
  18. * 在多线程下要使用Handler进行操作UI 利用回调接口的方式进行更新UI 
  19. * </p> 
  20. * @param imageUrl 
  21. * @param callback 
  22. * @return 
  23. */ 
  24. public Drawable loadDrawable(final String imageUrl, final ImageCallback callback) { 
  25. //进行判断ImageCache中是否存在缓存图片 
  26. if (imageCache.containsKey(imageUrl)) { 
  27. SoftReference<Drawable> softReference = imageCache.get(imageUrl); 
  28. if (softReference.get() != null) { 
  29. return softReference.get(); 
  30. //定义操作UI的Handler 
  31. final Handler handler = new Handler() { 
  32. @Override 
  33. public void handleMessage(Message msg) { 
  34. callback.imageLoaded((Drawable) msg.obj); 
  35. }; 
  36.  
  37. new Thread(new Runnable() { 
  38. @Override 
  39. public void run() { 
  40. Drawable drawable = loadImageFromUrl(imageUrl); 
  41. imageCache.put(imageUrl, new SoftReference<Drawable>(drawable)); 
  42. Message message = handler.obtainMessage(0, drawable); 
  43. handler.sendMessage(message); 
  44. }).start(); 
  45. return null
  46.  
  47. //根据URL地址进行获取资源 
  48. protected Drawable loadImageFromUrl(String imageUrl) { 
  49. try { 
  50. return Drawable.createFromStream(new URL(imageUrl).openStream(), "src"); 
  51. catch (Exception e) { 
  52. throw new RuntimeException(); 
  53.  
  54. //回调接口 
  55. public interface ImageCallback { 
  56. public abstract void imageLoaded(Drawable drawable); 

主Activity:

  1. /** 
  2. * User: Tom 
  3. * Date: 13-5-13 
  4. * Time: 下午8:33 
  5. */ 
  6. public class LoadImage extends Activity { 
  7. private AsnycImageLoader loader = null
  8.  
  9. public void onCreate(Bundle savedInstanceState) { 
  10. super.onCreate(savedInstanceState); 
  11. setContentView(R.layout.loadimages); 
  12.  
  13. loader = new AsnycImageLoader(); 
  14.  
  15. loadImage("http://www.jb51.net/images/icon-partners.png", R.id.image1); 
  16. loadImage("http://www.jb51.net/images/icon-dev.png", R.id.image2); 
  17. loadImage("http://pic28.jb51.net/20130421/12302174_231210305323_2.jpg", R.id.image3); 
  18.  
  19.  
  20.  
  21. public void loadImage(String url, int id) { 
  22. final ImageView imageView = (ImageView) findViewById(id); 
  23. Drawable cacheImage = loader.loadDrawable(url, new AsnycImageLoader.ImageCallback() { 
  24. @Override 
  25. public void imageLoaded(Drawable drawable) { 
  26. imageView.setImageDrawable(drawable); 
  27. }); 
  28. if (cacheImage != null) { 
  29. imageView.setImageDrawable(cacheImage); 
--转自http://mobile.51cto.com/abased-400973.htm--
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值