当前位置:操作系统 > 安卓/Android >>

android TraceView使用以及listview 的性能优化测试(二)

上篇最后面给了我测试用的代码: 未看到请先浏览上一篇:http://www.zzzyk.com/kf/201201/116681.html

好现在咋门来验证listView 的神奇,listview主要需要优化的就是getView() 这个方法,实现其中convertView 的缓存优化,下面就利用TraceView 测试下listview重用convertView  的区别:

 首先啥都不做:

   

[html] public View getView(int position, View convertView, ViewGroup parent) { 
        // TODO Auto-generated method stub 
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.listview, null); 
        TextView text = (TextView) layout.findViewById(R.id.text); 
        ImageView view= (ImageView) layout.findViewById(R.id.iamge); 
         
        text.setText(listData.get(position)); 
        int id= position %2==1? R.drawable.icon: R.drawable.default_head; 
        view.setImageResource(id); 
         
        return layout; 
    } 
public View getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.listview, null);
  TextView text = (TextView) layout.findViewById(R.id.text);
  ImageView view= (ImageView) layout.findViewById(R.id.iamge);
  
  text.setText(listData.get(position));
  int id= position %2==1? R.drawable.icon: R.drawable.default_head;
  view.setImageResource(id);
  
  return layout;
 }

运行程序,然后随意的拖动listview 列表,然后安菜单键退出程序:   去ddms 中fileExplorer中 点击sd卡 你会早根目录上看到dmtrace.trace 文件

 把dmtrace.trace 导出到C盘 ,命令行键入android  tools 文件夹下 执行  traceview C:\ dmtrace.trace   出现了traceview   视图

因为咋门看到的是getview优化操作:所以直接在Find: 键入getView 他则会找到我们写的程序的方法:

 \

 

看到没有:未进行优化的情况下:getView占用资源是 35.2%   其中布局填充(inflate)占其中的89.7%    整个程序中inflated 就占33%,getView()方法就是全被布局填充耗费了这么多的资源, 看不下去了

优化一

直接加两行代码:

[java] if(convertView ==null){ 
        layout = (LinearLayout) inflater.inflate(R.layout.listview, null); 
    }else{ 
        layout =(LinearLayout) convertView; 
    } 
 if(convertView ==null){
   layout = (LinearLayout) inflater.inflate(R.layout.listview, null);
  }else{
   layout =(LinearLayout) convertView;
  }
  在运行 ,在查看getview:

 \

 

看到没有,看到没有:9.4%  占整个程序的9.4% ,并且 inflated 在getview中只耗费了41.7%了,一半多的节省啊!

两行的代码就带来这么大的效率提高: 难道你没觉察到! 神奇

 

优化二

下面是网上盛传的:ViewHolder 优化测试 通过setTAG

[java] public View getView(int position, View convertView, ViewGroup parent) { 
        // TODO Auto-generated method stub  
//      LinearLayout layout;  
//      if(convertView ==null){  
//          layout = (LinearLayout) inflater.inflate(R.layout.listview, null);  
//      }else{  
//          layout =(LinearLayout) convertView;  
//      }  
//        
//      TextView text = (TextView) layout.findViewById(R.id.text);  
//      ImageView view= (ImageView) layout.findViewById(R.id.iamge);  
//        
//      text.setText(listData.get(position));  
//      int id= position %2==1? R.drawable.icon: R.drawable.default_head;  
//      view.setImageResource(id);  
         
         ViewHolder holder; 
         if (convertView == null) { 
             convertView = inflater.inflate(R.layout.listview, 
                     null); 
             holder = new ViewHolder(); 
             holder.view = (ImageView) convertView.findViewById(R.id.iamge); 
             holder.text = (TextView) convertView.findViewById(R.id.text); 
             convertView.setTag(holder); 
         } 
         else{ 
             holder = (ViewHolder)convertView.getTag(); 
         } 
         int id= position %2==1? R.drawable.icon: R.drawable.default_head; 
         holder.view.setImageResource(id); 
         holder.text.setText(listData.get(position)); 
 
         
        return convertView; 
    } 
     static class ViewHolder { 
            TextView text; 
            ImageView view; 
        } 
public View getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
//  LinearLayout layout;
//  if(convertView ==null){
//   layout = (LinearLayout) inflater.inflate(R.layout.listview, null);
//  }else{
//   layout =(LinearLayout) convertView;
//  }
//  
//  TextView text = (TextView) layout.findViewById(R.id.text);
//  ImageView view= (ImageView) layout.findViewById(R.id.iamge);
//  
//  text.setText(listData.get(position));
//  int i

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,