Understanding Android Canvas Clipping

Batdude picture Batdude · Jun 8, 2012 · Viewed 8k times · Source

I am having trouble finding an answer to this. Consider the clipping code below:

           boolean is_ok = mycanvas.clipRect(clip_left, clip_top, clip_right+1, clip_bottom+1);
       mycanvas.getClipBounds(clipRect);
       if (!is_ok ||
               clipRect.left != clip_left ||
               clipRect.top != clip_top ||
               clipRect.right != clip_right+1 ||
               clipRect.bottom != clip_bottom+1)
       {
           Log.i("DEBUG", "setClipping failed");
       }

When the clip bounds are returned they don't match what was just set. For example if clip_left, clip_top, clip_right, clip_bottom are (100,50,109, 59) then I would expect the clipping bounds to be (100, 50, 110, 60) given the code above. It isn't. getClipBounds() returns (100, 51, 110, 60).

Why is top = 51 when I just set it to 50? There's something under the hood I don't understand yet.

Answer

Batdude picture Batdude · Jun 8, 2012

OK, I need to read more clearly before asking questions. In case anyone is interested I'll answer this myself:

When setting a new clipRect, I assumed it would replace the prior clipping. This is NOT so. Instead it creates an intersection with the previous clipping. From the Android Developers page:

clipRect(float left, float top, float right, float bottom)

Intersect the current clip with the specified rectangle, which is expressed in local coordinates.