I have a Layout which as a ImageView, ListView, some TextViews etc wrapped in a ScrollView. Whenever the user scrolls the Parent View calls all the child view’s draw() methods. Since my ImageView is a complex bitmap constructed on the fly the scroll really becomes laggy because my imageview’s draw() is called continuously, which delagates to ImageView.onDraw() and which in turn delegates to my complex ConstructMyBitmap.draw() and redraws my bitmap every single time.
I need to suppress this imageView.onDraw() call on scrollevent. I dont want to redraw my complex bitmap everytime when a scroll happens.
How to identify scrollevent inside onDraw()?
Or is there a better way to handle this?
To Romain Guy:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(chart != null && chart.shdRedraw())
{
chart.draw(canvas);
}else
{
//Do nothing - Canvas should retain the old data
}
// :-) Happy, Now I control when the chart shd be drawn but.. :-(
}
Unfortunately when onDraw() gets called onScroll event and I decide not to draw the chart the result is a blank chart area. The canvas given to me in the onDraw() doesnt contain the previously drawn information. Dont know whether onDraw() flushes the canvas? Any guidance on this?
The screen needs to be redraw on a scroll so the only way to achieve what you want is to cache your View (using View’s drawingCacheEnabled property.) This is however costly in terms of memory. Why don’t you simply change your draw() code to only recreate your expensive bitmap when it has changed?