Making the Android UI Fast and Efficient by Romain Guy

Point!
Adapters
  • Better Code for efficient view redraw
——————————————————————–
1. Create simple class
——————————————————————–
static class ViewHolder {
TextView text;
ImageView icon;
}

——————————————————————–

2. getView()
——————————————————————–
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
——————————————————————–
  • Backgrounds and images
——————————————————————–
1. Create twice bigger size of background image
——————————————————————–
2. Pre-scaling the image
——————————————————————–
originalImage = Bitmap.createScaledBitmap(
originalImage,
view.getWidth(),
view.getHeight(),
true
);
——————————————————————–
Or
1. Remove the background completely to prevent redraw
——————————————————————–
<!– res/values/styles.xml –>
<resources>
<style name=”Theme.NoBackground” parent=”android:Theme”>
<item name=”android:windowBackground”>@null</item>
</style>
</resources>
——————————————————————–
<activity
android:name=”MyApp”
android:theme=”@style/NoBackgroundTheme”>
<!– intent filters and stuff –>
</activity>
  • Drawing and invalidating
——————————————————————–
Instead of using invalidate(), which is expensive, use regional invalidate()
——————————————————————–
– invalidate(Rect)
– invalidate(left, top, right, bottom)
……………

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.