How to Search a 2D Array? • Use «Static» as Possible • «Lint» Usage • Reusable Layouts • Do Your Hard-Work on Background Threads • View Holder for ListView Objects • XML Drawables • Handling Overdraws • Summary and Little Other Tips
EXPENSIVE! … but «primitive» is not! Using a 2 different integer array is more efficient than using object arrays like fooBar(int,int) badArray = new FooBar[] { new FooBar(5,8) , new FooBar(84,2) }; firstCoolArray = new int[] { 5, 84 }; secondCoolArray = new int[] { 8 , 2 };
then column TO DO for (int i = 0; i < foo.length ; i++) { for (int j = 0; j < foo.length ; j++) { process(foo[i,j]); } } NOT TO DO for (int i = 0; i < foo.length ; i++) { for (int j = 0; j < foo.length ; j++) { process(foo[j,i]); } }
files for potential bugs and optimization improvements for correctness, security, performance, usability, accessibility, and internationalization. * http://developer.android.com/tools/debugging/improving-w-lint.html
bars, yes-no button panels etc. Very easy to implement: Create a layout and use it in another layout with «<include ../>» tag Reusing a layout is like using static for layouts.
make network processes in main thread, since Android 3.0 Phones and tablets have great ability for parallel process, anymore. So why not use it? How about Instagram? When you finished writing some hashtags and pressed to send button, It had been posted just a while ago!
text; TextView timestamp; ImageView icon; } ViewHolder holder = new ViewHolder(); holder.icon = (ImageView) convertView.findViewById(R.id.listitem_image); holder.text = (TextView) convertView.findViewById(R.id.listitem_text); … A ViewHolder object stores each of the component views inside the ListView object, so you can immediately access them without the need to look them up repeatedly.
a hard work for mobile device. But creating an image with XML is very cheap and it is more efficient than you expected. Using a 400 kb background image or using an Xml drawable which has size of just 300 bytes?
you ask me! Since Android 4.2, we can see overdraws in our devices. Overdraw is simply, drawing a pixel more than one. It’s notation is also simple: • No color: No overdraw • Blue: 1x overdraw (pixel is drawed 2 times) • Green: 2x overdraw • Light Red: 3x overdraw • Dark Red: 4x or more overdraw (ALERT!)
and deeper, it is engineering miracle! But there are something to do from upper layer: • thumbnail.setBackgroundColor(0x0); • android:windowBackground="@null" • If a view will not used anymore, don’t make its visibility «INVISIBLE», make it «GONE» • And use a simple design!
«int» arrays instead of array of objects (int,int) Do use «primitive» instead of objects Do use as possible as «static» methods Do use «static final» for constants Do use foreach instead of for Do use as possible as Xml Drawables instead of images (jpg,png) Don’t create unnecessary objects Don’t allocate memory if you can work without it Don’t use getters and setters inside the class Don’t do your hard works on main thread and the most important one: Don’t publish your app unless it doesn’t have performance problems!