这里有新鲜出炉的精品教程,程序狗速度看过来!
Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。
这篇文章主要介绍了Android实现ListView异步加载的方法,针对前面介绍的方法进行了线程操作的改进,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了Android实现ListView异步加载的方法。分享给大家供大家参考,具体如下:
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- …………
- ViewHolder VH = null;
- …………
- VH.mImageView.setTag(position);
- VH.mThumb.setImageDrawable(imageLoader.loadDrawable(position,
- new ImageCallback() {
- public void imageLoaded(Drawable imageDrawable, int position) {
- ImageView imageViewByTag = (ImageView) mList.findViewWithTag(position);
- if (imageViewByTag != null) {
- imageViewByTag.setImageDrawable(imageDrawable);
- }
- }
- }));
- }
- private static LruCache<Long, Drawable> mCache = new LruCache<Long, Drawable>(100);
- public class AsyncImageLoader {
- public Drawable loadDrawable(final int position, final ImageCallback callback){
- Drawable d = null;
- d = mCache.get(position);
- if (d == null) {
- final Handler handler=new Handler(){
- @Override
- public void handleMessage(Message msg) {
- callback.imageLoaded((Drawable) msg.obj, position);
- }
- };
- Thread t = new Thread(){
- public void run() {
- Drawable drawable = Utils.getDrawable(mContext, position, 1, mMyDefaultIcon);
- Drawable value = mCache.get(position);//cache有可能已经被更改了,所以重新取一次
- if (value == null) {
- mCache.put(position, drawable);
- } else {
- drawable = value;
- }
- handler.sendMessage(handler.obtainMessage(0,drawable));
- };
- };
- t.setPriority(Thread.MIN_PRIORITY);
- t.start();
- return mMyDefaultIcon;
- } else {
- return d;
- }
- }
- }
- public interface ImageCallback{
- public void imageLoaded(Drawable imageDrawable, int position);
- }
这个是在前面一篇/article/17/0913/348311.html基础上修改的。主要为了加入LruCache
后来发现,这种方法要不停的开辟新的线程,效率并不是很高。最后改为加入一后台线程,不停从后进先出队列中取出任务进行处理。
希望本文所述对大家Android程序设计有所帮助。
来源: http://www.phperz.com/article/17/0917/348310.html