您的位置:首页 > 移动开发 > Android开发

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

2011-12-29 17:58 513 查看
 接上篇的 android TraceView使用以及listview 的性能优化测试(一)

上篇最后面给了我测试用的代码: 未看到请先浏览上一篇:

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

 首先啥都不做:

   

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()方法就是全被布局填充耗费了这么多的资源, 看不下去了

优化一

直接加两行代码:

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

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;
}

  测试效果是比 优化一 好点点: 主要在于findviewbyID 比findviewbyTag少了很多



优化三

if (convertView == null) {
convertView = inflater.inflate(R.layout.listview, null);
convertView.setTag(R.id.text, convertView.findViewById(R.id.text));
convertView.setTag(R.id.iamge, convertView.findViewById(R.id.iamge));
}
((TextView) convertView.getTag(R.id.text)).setText(listData.get(position));
int id= position %2==1? R.drawable.icon: R.drawable.default_head;
((ImageView) convertView.getTag(R.id.iamge)).setImageResource(id);




 大家也看到了。跟优化一是差不多的最终的效果

也许结果不是很准确,但至少我们知道了怎么使用traceView 来帮助我们,同时也知道了listview一定的优化,具体采用哪种优化还需要看具体的需求,

对于性能要求很高的 推荐,优化二,对于一般的应用优化一足够了,再有就是settag 和findbyid 使用情况,经过测试证明了:

View本身因为setTag而会占用更多的内存,还会增加代码量;而findViewById会临时消耗更多的内存,所以不可盲目使用,依实际情况而定。

 

希望对你有帮助!

 

 

 

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息