Android优化系列——控件优化(ListView 异步加载图片优化,SoftReference)

整理来自Android优化技术详解一书

ListView控件优化

网络上有很多关于ListView优化的博文,这里就不详细讲述了

  • 重写getView方法中,if语句判断convertView是否为空,如果不为空,调用getTag()方法
ViewHolder holder;
if(convertView == null){
//这里初始化holder类中变量
...
convertView.setTag(holder);
}else{
    holder = (ViewHolder)convertView.getTag();
}
  • 创建内部类ViewHolder
static class ViewHolder{
    TextView text1;
    ...
}

以上代码同样适用于重写Adapter。

ListView 异步加载图片优化

  • 主要方法
package cn.xxx.test;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;

public class AsyncImageLoader {

     private HashMap<String, SoftReference<Drawable>> imageCache;

         public AsyncImageLoader() {
             imageCache = new HashMap<String, SoftReference<Drawable>>();
         }

         public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {
             if (imageCache.containsKey(imageUrl)) {
                 //SoftReference 是软引用,具体介绍在后面
                 SoftReference<Drawable> 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>(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;
        }
          //这里是一个回调接口,imageLoaded方法在后面的代码中实现
         public interface ImageCallback {
             public void imageLoaded(Drawable imageDrawable, String imageUrl);
         }

}

上述代码是实现异步获取图片的主方法,SoftReference 是软引用,目的是更好的实现系统回收变量,重复的URL直接返回已有的资源,实现回调函数,让数据成功后,更新到UI线程。
SoftReference的特点是它的一个实例保存对一个Java对象的软引用,该软引用的存在不妨碍垃圾收集线程对该Java对象的回收。也就是说,一旦SoftReference保存了对一个Java对象的软引用后,在垃圾线程对这个Java对象回收前,SoftReference类所提供的get()方法返回Java对象的强引用。另外,一旦垃圾线程回收该Java对象之后,get()方法将返回null。
SoftReferences 只有在 jvm Out Of Memory 之前才会被回收, 所以它非常适合缓存应用。

  • 辅助类文件ImageAndText,类似持久化类
package cn.xxx.test;

public class ImageAndText {
        private String imageUrl;
        private String text;

        public ImageAndText(String imageUrl, String text) {
            this.imageUrl = imageUrl;
            this.text = text;
        }
        public String getImageUrl() {
            return imageUrl;
        }
        public String getText() {
            return text;
        }
}
  • 辅助类文件ViewCache,用途类似ViewHolder。
package cn.xxx.test;

import cn.xxx.test.R;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class ViewCache {

        private View baseView;
        private TextView textView;
        private ImageView imageView;

        public ViewCache(View baseView) {
            this.baseView = baseView;
        }

        public TextView getTextView() {
            if (textView == null) {
                textView = (TextView) baseView.findViewById(R.id.text);
            }
            return textView;
        }

        public ImageView getImageView() {
            if (imageView == null) {
                imageView = (ImageView) baseView.findViewById(R.id.image);
            }
            return imageView;
        }

}
  • 实现ListView的Adapter类
package cn.xxx.test;

import java.util.List;

import cn.xxx.test.R;
import cn.xxx.test.AsyncImageLoader.ImageCallback;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> {

        private ListView listView;
        private AsyncImageLoader asyncImageLoader;

        public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts, ListView 

listView) {
            super(activity, 0, imageAndTexts);
            this.listView = listView;
            asyncImageLoader = new AsyncImageLoader();
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            Activity activity = (Activity) getContext();

            // Inflate the views from XML
            View rowView = convertView;
            ViewCache viewCache;
            if (rowView == null) {
                LayoutInflater inflater = activity.getLayoutInflater();
                rowView = inflater.inflate(R.layout.image_and_text_row, null);
                viewCache = new ViewCache(rowView);
                rowView.setTag(viewCache);
            } else {
                viewCache = (ViewCache) rowView.getTag();
            }
            ImageAndText imageAndText = getItem(position);

            // Load the image and set it on the ImageView
            String imageUrl = imageAndText.getImageUrl();
            ImageView imageView = viewCache.getImageView();
            imageView.setTag(imageUrl);
            //这里是最关键的回调函数的使用,当线程加载完图片时,通知handler调用回调函数,回调函数再调用以下代码
            Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
                public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                    ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
                    if (imageViewByTag != null) {
                        imageViewByTag.setImageDrawable(imageDrawable);
                    }
                }
            });
            if (cachedImage == null) {
                imageView.setImageResource(R.drawable.default_image);
            }else{
                imageView.setImageDrawable(cachedImage);
            }
            // Set the text on the TextView
            TextView textView = viewCache.getTextView();
            textView.setText(imageAndText.getText());

            return rowView;
        }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值