Android traceview usage and listview Performance Optimization Test (2)

Source: Internet
Author: User

Subsequent Android traceview usage and listview performance optimization tests (1)

Last part of the previous article gave me the code for testing: if you do not see it, please first browse the previous article:

Now let's verify the magic of listview. The main thing that listview needs to optimize is the getview () method, which implements the convertview Cache Optimization, the difference between using listview to reuse convertview in traceview is as follows:

First, do nothing:

public View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubLinearLayout 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;}

Run the program, drag the listview list at will, and then run the menu key to exit the program: Go to fileexplorer in ddms and click the SD card. You will see the dmtrace. trace file in the early root directory.

Export dmtrace. Trace to drive C, and run traceview c: \ dmtrace. trace in the android Tools Folder to display the traceview.

The getview optimization operation is displayed at the gate. Therefore, if you directly type getview in "find:", the method of the program we wrote will be found:

No: If no optimization is performed, getview occupies 35.2% of the resources. Inflate occupies 89.7% of the total resources. inflated occupies 33% of the total projects, and getview () occupies of the resources () the method is to be completely filled by layout, which consumes so many resources and cannot be viewed.

Optimization 1

Add two lines of code directly:

if(convertView ==null){layout = (LinearLayout) inflater.inflate(R.layout.listview, null);}else{layout =(LinearLayout) convertView;}

At run, view getview:

I can't see it. I don't see: 9.4% accounts for 9.4% of the entire program, and inflated only consumes 41.7% in getview, saving more than half!

The two lines of code bring about such a huge increase in efficiency: Don't you realize it! Magic

 

Optimization 2

The following is an online event: viewholder optimization test passed 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;    }

The test result is better than the optimization: The findviewbyid is much less than the findviewbytag.

Optimization 3

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

As you can see. The final result is similar to optimization 1.

Maybe the results are not very accurate, but at least we know how to use traceview to help us, and also know the optimization of listview. The specific optimization needs to be determined,

For recommendation with high performance requirements, optimization 2 is sufficient for general application optimization, and the use of settag and findbyid has been tested and proved:

View itself occupies more memory and increases the amount of code because of settag. findviewbyid temporarily consumes more memory, so it cannot be used blindly, depending on the actual situation.

 

Hope to help you!

 

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.