views:

25

answers:

1

I have subclassed View and do some bitmap drawing inside of the onDraw method. This view is then placed in a horizontal scrollview. Some of the time the bitmaps will not be visible since they are scrolled off screen. To improve performance I would like to avoid drawing anything when the object will not be visible.

So the question is, how do I determine that my bitmap will be drawn offscreen so I can just return without drawing?

A: 

I solved this by:

Rect s = new Rect();
getLocalVisibleRect(s);

// (...)
// Do not draw if outside screen
Rect b = getBounds();
boolean offScreen = b.left > s.right || b.right < s.left || b.top > s.bottom || b.bottom < s.top;
if (!offScreen) {
    // Draw here
}
icecream