- 浏览: 16995 次
- 性别:
- 来自: 南京
最新评论
在应用开发中,经常用到ListView去加载数据,加载图片和文字是比较常见的,文字还好,图片从网络请求加载速度比较慢,所以需要把图片的加载放到另一个线程中去执行,执行完了再更新UI线程。以下列出一个我在项目中使用到的异步加载图片的解决方案,代码没有上全,给出核心部分。
大致思路是这样:
1.利用软引用来缓存图片Bitmap,用图片的URL作为缓存查找的Key;
2.设两级缓存,一级是SoftReference,二级是本地SD卡;
3.如果两级缓存都没取到图片,则从服务器获取,并加入缓存;
4.加载完后通过回调接口通知UI更新;
以下是异步加载的关键代码,其中一些工具类没有给出,自己实现就可以,比如HttpRequest是我自己写的一个类。
[java] view plaincopy
01.public class AsyncImageLoader {
02.
03. //Cache for image(Type String is the URL of image,the second parameter is soft reference)
04. private HashMap<String, SoftReference<Bitmap>> imageCache = null;
05. private Activity context;
06.
07. public AsyncImageLoader(Activity context){
08. this.context = context;
09. imageCache = new HashMap<String, SoftReference<Bitmap>>();
10. }
11.
12. public Bitmap loadImage(final ImageView imageView,final String imageURL,final ImageCallBack imageCallBack){
13.
14. //If the cache contains the reference of bitmap then return
15. if (imageCache.containsKey(imageURL)) {
16. SoftReference<Bitmap> bitmapReference = imageCache.get(imageURL);
17. Bitmap bitmap = bitmapReference.get();
18. if (bitmap != null) {
19. return bitmap;
20. }
21. }
22. //Second cache,search local SD card
23. else {
24. String fileName = StringUtil.namePicture(imageURL);//获取文件名
25. boolean isExist = SystemUtils.findPhotoFromSDCard(Constant.INFO_PATH, fileName);
26. if (isExist) {//是否在SD卡存在图片
27. Bitmap bitmap = SystemUtils.getPhotoFromSDCard(Constant.INFO_PATH, fileName);
28. return bitmap;
29. }
30.
31. }
32.
33. final Handler myHandler = new Handler(){
34.
35. @Override
36. <span style="white-space: pre;"> </span> public void handleMessage(Message msg)
37. <span style="white-space: pre;"> </span>{
38. <span style="white-space: pre;"> </span>imageCallBack.setImage(imageView, (Bitmap)msg.obj);
39. <span style="white-space: pre;"> </span>}
40. };
41.
42. //If the bitmap not exists in cache or SD card,then get it from net
43. new Thread(){
44.
45. @Override
46. public void run() {
47. // TODO Auto-generated method stub
48. boolean isNetwork = SystemUtils.checkNetwork(context);
49. if (isNetwork) {
50. InputStream photoStream = HttpRequest.getImageStream(imageURL);//这里是我自己写的一个类,目的是通过URL地址从服务器获取图片输入流
51. Bitmap bitmap;
52. try {
53. bitmap = ImageTools.getResizeBitmap(photoStream, 128, 128);
54. if (bitmap != null) {
55. String fileName = StringUtil.namePicture(imageURL);
56. //Save image to SD card
57. SystemUtils.savePhotoToSDCard(bitmap, fileName, Constant.INFO_PATH);
58. //Put soft reference to cache
59. imageCache.put(imageURL, new SoftReference<Bitmap>(bitmap));
60. //Send message to update UI
61. Message message = myHandler.obtainMessage(0, bitmap);
62. myHandler.sendMessage(message);
63. }
64. } catch (Exception e) {
65. // TODO Auto-generated catch block
66. e.printStackTrace();
67. }
68. }
69. }
70. }.start();
71.
72. return null;
73. }
74.
75. /**
76. * Interface for load image
77. * @author Ryan
78. *
79. */
80. public interface ImageCallBack{
81. //Set image for imageview through bitmap
82. public void setImage(ImageView imageView,Bitmap bitmap);
83. }
84.
85.}
在ListView的adapter的getView方法中:
[java] view plaincopy
01.Bitmap bitmap1 = asyncImageLoader.loadImage(viewHolder.imageView1, url1, new ImageCallBack() {
02.
03. @Override
04. public void setImage(ImageView imageView, Bitmap bitmap) {
05. // TODO Auto-generated method stub
06. imageView.setImageBitmap(bitmap);
07. }
08. });
09.
10. if (bitmap1 != null) {
11. viewHolder.imageView1.setImageBitmap(bitmap1);
12. }else {
13. viewHolder.imageView1.setImageResource(R.drawable.image_bg);
14. }
其中asyncImageLoader是在adapter的构造方法中初始化的,形成一个缓存。通过这个机制就可以实现ListView的图片异步加载,在用户体验上比直接加载要感觉好很多,那样会造成界面卡顿。这里是加载一张图片的情况,如果ListView的item中的图片是不定的,有可能是一张、两张、三张,该用什么方式呢,大家
大致思路是这样:
1.利用软引用来缓存图片Bitmap,用图片的URL作为缓存查找的Key;
2.设两级缓存,一级是SoftReference,二级是本地SD卡;
3.如果两级缓存都没取到图片,则从服务器获取,并加入缓存;
4.加载完后通过回调接口通知UI更新;
以下是异步加载的关键代码,其中一些工具类没有给出,自己实现就可以,比如HttpRequest是我自己写的一个类。
[java] view plaincopy
01.public class AsyncImageLoader {
02.
03. //Cache for image(Type String is the URL of image,the second parameter is soft reference)
04. private HashMap<String, SoftReference<Bitmap>> imageCache = null;
05. private Activity context;
06.
07. public AsyncImageLoader(Activity context){
08. this.context = context;
09. imageCache = new HashMap<String, SoftReference<Bitmap>>();
10. }
11.
12. public Bitmap loadImage(final ImageView imageView,final String imageURL,final ImageCallBack imageCallBack){
13.
14. //If the cache contains the reference of bitmap then return
15. if (imageCache.containsKey(imageURL)) {
16. SoftReference<Bitmap> bitmapReference = imageCache.get(imageURL);
17. Bitmap bitmap = bitmapReference.get();
18. if (bitmap != null) {
19. return bitmap;
20. }
21. }
22. //Second cache,search local SD card
23. else {
24. String fileName = StringUtil.namePicture(imageURL);//获取文件名
25. boolean isExist = SystemUtils.findPhotoFromSDCard(Constant.INFO_PATH, fileName);
26. if (isExist) {//是否在SD卡存在图片
27. Bitmap bitmap = SystemUtils.getPhotoFromSDCard(Constant.INFO_PATH, fileName);
28. return bitmap;
29. }
30.
31. }
32.
33. final Handler myHandler = new Handler(){
34.
35. @Override
36. <span style="white-space: pre;"> </span> public void handleMessage(Message msg)
37. <span style="white-space: pre;"> </span>{
38. <span style="white-space: pre;"> </span>imageCallBack.setImage(imageView, (Bitmap)msg.obj);
39. <span style="white-space: pre;"> </span>}
40. };
41.
42. //If the bitmap not exists in cache or SD card,then get it from net
43. new Thread(){
44.
45. @Override
46. public void run() {
47. // TODO Auto-generated method stub
48. boolean isNetwork = SystemUtils.checkNetwork(context);
49. if (isNetwork) {
50. InputStream photoStream = HttpRequest.getImageStream(imageURL);//这里是我自己写的一个类,目的是通过URL地址从服务器获取图片输入流
51. Bitmap bitmap;
52. try {
53. bitmap = ImageTools.getResizeBitmap(photoStream, 128, 128);
54. if (bitmap != null) {
55. String fileName = StringUtil.namePicture(imageURL);
56. //Save image to SD card
57. SystemUtils.savePhotoToSDCard(bitmap, fileName, Constant.INFO_PATH);
58. //Put soft reference to cache
59. imageCache.put(imageURL, new SoftReference<Bitmap>(bitmap));
60. //Send message to update UI
61. Message message = myHandler.obtainMessage(0, bitmap);
62. myHandler.sendMessage(message);
63. }
64. } catch (Exception e) {
65. // TODO Auto-generated catch block
66. e.printStackTrace();
67. }
68. }
69. }
70. }.start();
71.
72. return null;
73. }
74.
75. /**
76. * Interface for load image
77. * @author Ryan
78. *
79. */
80. public interface ImageCallBack{
81. //Set image for imageview through bitmap
82. public void setImage(ImageView imageView,Bitmap bitmap);
83. }
84.
85.}
在ListView的adapter的getView方法中:
[java] view plaincopy
01.Bitmap bitmap1 = asyncImageLoader.loadImage(viewHolder.imageView1, url1, new ImageCallBack() {
02.
03. @Override
04. public void setImage(ImageView imageView, Bitmap bitmap) {
05. // TODO Auto-generated method stub
06. imageView.setImageBitmap(bitmap);
07. }
08. });
09.
10. if (bitmap1 != null) {
11. viewHolder.imageView1.setImageBitmap(bitmap1);
12. }else {
13. viewHolder.imageView1.setImageResource(R.drawable.image_bg);
14. }
其中asyncImageLoader是在adapter的构造方法中初始化的,形成一个缓存。通过这个机制就可以实现ListView的图片异步加载,在用户体验上比直接加载要感觉好很多,那样会造成界面卡顿。这里是加载一张图片的情况,如果ListView的item中的图片是不定的,有可能是一张、两张、三张,该用什么方式呢,大家
发表评论
-
Android应用如何监听自己是否被卸载及卸载反馈功能的实现(第三版)
2015-01-26 21:04 685两个月前发了两篇有关 ... -
移动端与PHP服务端接口通信流程设计(增强版)
2014-11-21 14:37 553前面讲过:移动端与PHP服务端接口通信流程设计(基础版) 对 ... -
移动端与PHP服务端接口通信流程设计(基础版)
2014-11-21 14:19 631针对 --->非开放性平台 --->公司内部产 ... -
Android WebView与JS交互及消息处理
2014-11-06 19:07 651前一段时间做公司的项目,涉及到WebView与JS之间的通信, ... -
解决video标签在安卓webview下无法自动播放问题
2014-11-05 19:50 2038安卓webview下 html5 的 video 设置au ... -
android 为一个apk设置多个ICON图标和执行入口
2014-07-12 11:34 1030在Android开发中,一个工程对应一个AndroidMani ... -
如何在Android中启动JAVA程序
2014-05-27 20:20 424在Android中启动JAVA程序 ... -
Android root的两种方法-udev漏洞和setuid漏洞
2014-05-19 19:54 881转载自:http://intrepidusgroup.com/ ... -
Android Root 1 —— RageAgainstTheCage
2014-05-19 19:45 1141这个漏洞网上有很多资 ... -
android 使用include 调用内部组件
2014-05-09 10:44 561例子一: sublayout.xml <?xml ... -
Android系统设计中存在设计模式分析
2013-12-18 18:52 7431、 开闭(OCP) 简介:要扩展,不要修改。比如我对外提供 ...
相关推荐
android listview异步加载图片实例 用到了线程池 下载的图片会保存到本地 并在数据库中保留记录 再次加载时会直接从本地读取
listview 异步加载图片
Android Listview异步加载图片,图片错位解决方案
Android实现ListView异步加载图片
Android listview异步加载图片
实现了异步加载图片进度条,代码更具有规范性
Android 异步加载图片,对ListView的异步加载图片的功能演示,主要根据url读取图片返回流的方法。为了方便演示,将请求图片的链接先固定,每读取好一个图片就更新,界面比较简单,当然你可以做成比较好的,像很多好...
android中ListView异步加载图片时的图片错位问题解决方案
listView异步加载图片实例 简单实例
ListView异步加载图片和文字,避免内存溢出和滑动效率.