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 impr开发者_JAVA百科ove 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?
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
}
精彩评论