展会信息港展会大全

android TraceView使用以及listview 的性能优化测试(二)
来源:互联网   发布日期:2015-11-27 13:36:40   浏览:2410次  

导读:上篇最后面给了我测试用的代码: 未看到请先浏览上一篇:http://www.2cto.com/kf/201201/116681.html好现在咋门来验证listView 的神奇,listvi...

上篇最后面给了我测试用的代码: 未看到请先浏览上一篇:http://www.2cto.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盘 ,命令行键入androidtools 文件夹下 执行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 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会临时消耗更多的内存,所以不可盲目使用,依实际情况而定。

希望对你有帮助!

摘自 android小益的专栏

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港