Search code examples
androidimagecanvasscrollviewblit

How can I determine if I will draw offscreen when doing custom bitmap blitting in a view inside a scrollview in Android


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?


Solution

  • 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
    }