新闻中心

EEPW首页>嵌入式系统>设计应用> android ListView异步加载图片

android ListView异步加载图片

作者: 时间:2016-09-12 来源:网络 收藏

ListView异步加载图片是非常实用的方法,凡是是要通过网络获取图片资源一般使用这种方法比较好,用户体验好,不用让用户等待下去,下面就说实现方法,先贴上主方法的代码

本文引用地址://m.amcfsurvey.com/article/201609/305008.htm

public class AsyncImageLoader {

private HashMap

public AsyncImageLoader() {

imageCache = new HashMap

}

public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {

if (imageCache.containsKey(imageUrl)) {

SoftReference softReference = imageCache.get(imageUrl);

Drawable drawable = softReference.get();

if (drawable != null) {

return drawable;

}

}

final Handler handler = new Handler() {

public void handleMessage(Message message) {

imageCallback.imageLoaded((Drawable) message.obj, imageUrl);

}

};

new Thread() {

@Override

public void run() {

Drawable drawable = loadImageFromUrl(imageUrl);

imageCache.put(imageUrl, new SoftReference (drawable));

Message message = handler.obtainMessage(0, drawable);

handler.sendMessage(message);

}

}.start();

return null;

}

public static Drawable loadImageFromUrl(String url) {

URL m;

InputStream i = null;

try {

m = new URL(url);

i = (InputStream) m.getContent();

} catch (MalformedURLException e1) {

e1.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

Drawable d = Drawable.createFromStream(i, src);

return d;

}

public interface ImageCallback {

public void imageLoaded(Drawable imageDrawable, String imageUrl);

}

}

实现方式:通过传入图片的网络地址和一个实现ImageCallback行为的对象,当imageCache存在这个图片时候,返回这个图片,当imageCache没有这个图片时,实例一个异步线程来下载图片并同时返回为null,最后在图片下载完成的时候,调用imageLoaded方法。

现说说这个类设计的优点吧:1.采用了策略模式;2.使用SoftReference关键字

先说说策略模式,程序里把每次下载图片完成后所进行的操作封装成一个ImageCallback抽象类,使系统更灵活,并易于扩展。

在Java中内存管理,引用分为四大类,强引用HardReference、弱引用WeakReference、软引用SoftReference和虚引用PhantomReference。它们的区别也很明显,HardReference对象是即使虚拟机内存吃紧抛出OOM也不会导致这一引用的对象被回收,而WeakReference等更适合于一些数量不多,但体积稍微庞大的对象,在这四个引用中,它是最容易被垃圾回收的,而我们对于显示类似Android Market中每个应用的App Icon时可以考虑使用SoftReference来解决内存不至于快速回收,同时当内存短缺面临Java VM崩溃抛出OOM前时,软引用将会强制回收内存,最后的虚引用一般没有实际意义,仅仅观察GC的活动状态,对于测试比较实用同时必须和ReferenceQueue一起使用。对于一组数据,我们可以通过HashMap的方式来添加一组SoftReference对象来临时保留一些数据,同时对于需要反复通过网络获取的不经常改变的内容,可以通过本地的文件系统或数据库来存储缓存。

最后一句话说的很对,事实上大多数情况也是如此。

在说说它的用法吧,通常它作为一个adapter的一个变量如:

class BookAdapter extends ArrayAdapter {

AsyncImageLoader asyncImageLoader;

Context mContext;

BookAdapter(Context context,List data){

super(context, 0, data);

asyncImageLoader = new AsyncImageLoader();

mContext = context;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ViewCache holder ;

if(convertView==null){

LayoutInflater inflate = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = inflate.inflate(com.slider.cn.R.layout.list_item , null);

holder = new ViewCache();

holder.icon = (ImageView)convertView.findViewById(com.slider.cn.R.id.note_icon);

holder.name = (TextView)convertView.findViewById(com.slider.cn.R.id.note_name);

holder.date = (TextView)convertView.findViewById(com.slider.cn.R.id.note_date);

convertView.setTag(holder);

}else{

holder = (ViewCache)convertView.getTag();

}

final BookInfo bookInfo = getItem(position);

holder.name.setText(bookInfo.getName().toString());

holder.date.setText(bookInfo.getInfo());

holder.icon.setTag(bookInfo.getUri());

//

Drawable drawable = asyncImageLoader.loadDrawable(bookInfo.getUri(), new ImageCallback() {

@Override

public void imageLoaded(Drawable imageDrawable, String imageUrl) {

ImageView imageViewByTag = (ImageView) BookListView.this.findViewWithTag(bookInfo.getUri());

if (imageViewByTag!=null) {

imageViewByTag.setImageDrawable(imageDrawable);

}else {

//load image failed from Internet

}

}

});

if(drawable==null){

holder.icon.setImageDrawable(drawable_waiting);


上一页 1 2 下一页

关键词:

评论


相关推荐

技术专区

关闭