Search This Blog

Thursday, 2 February 2012

Optimizing listview!!!

Well here is a short but beneficial tutorial on how to optimize the listview, many of the beginners asked me that if they set any color or background image in their listview the common problem they suffer is of flickering the background when they scroll the listview the color or the background image they added just disappears and after the scrolling make its end mark the background color appears immediately so the main logic behind this is the working of the listview i.e  ListViewredraws its children, it has to blend the children with the window's background. Once again, this requires costly readbacks from memory that are particularly painful during a scroll or a fling when drawing happens dozen of times per second.
To improve drawing performance during scrolling operations, the Android framework reuses the cache color hint. When this hint is set, the framework copies each child of the list in a Bitmap filled with the hint value (assuming that another optimization, called scrolling cache, is not turned off). ListView then blits these bitmaps directly on screen and because these bitmaps are known to be opaque, no blending is required. Also, since the default cache color hint is #191919, you get a dark background behind each item during a scroll.
To fix this issue, all you have to do is either disable the cache color hint optimization, if you use a non-solid color background, or set the hint to the appropriate solid color value. You can do this from code (see setCacheColorHint(int)) or preferably from XML, by using the android:cacheColorHint attribute. To disable the optimization, simply use the transparent color #00000000. The following screenshot shows a list with android:cacheColorHint="#00000000" set in the XML layout file:


well if you have any queries regarding this please drop the comments below !!!! 

No comments:

Post a Comment