Fill the canvas outside the rectangle

I want to fill the area outside the rectangle on the canvas. I use

 canvas.drawRect(pTopLeft.x, pTopLeft.y, pBotRight.x, pBotRight.y, paint);

to draw a rectangle but cannot figure out how to fill outside the rectangle / clip.

Thanks Geoff

+5
source share
4 answers

You are not going to fill out the clip; what kind of clip to prevent! If you want to fill the space outside the rectangle and inside the borders of the drawing layer, build four auxiliary rectangles:

Rect above = new Rect(0, 0, canvas.getWidth(), pTopLeft.y);
Rect left = new Rect(0, pTopLeft.y, pTopLeft.x, pBotRight.y);
Rect right = new Rect(pBotRight.x, pTopLeft.y, canvas.getWidth(), pBotRight.y);
Rect bottom = new Rect(0, pBotRight.y, canvas.getWidth(), canvas.getHeight());

Then fill them out.

+6
source

Thanks ted and trojanfoe - the best solution I came up with is

    Point pTopLeft = new Point();
    Point pBotRight = new Point();

    //TODO:set x,y for points

    Rect rHole = new Rect(pTopLeft.x, pTopLeft.y, pBotRight.x, pBotRight.y);
    //assume current clip is full canvas
    //put a hole in the current clip
    canvas.clipRect(rHole,  Region.Op.DIFFERENCE);
    //fill with semi-transparent red
    canvas.drawARGB(50, 255, 0, 0);
    //restore full canvas clip for any subsequent operations
    canvas.clipRect(new Rect(0, 0, canvas.getWidth(), canvas.getHeight())
                    , Region.Op.REPLACE);
+15
source

ICS ...

canvas.clipRect(rHole,  Region.Op.DIFFERENCE);

XOR, Difference ReverseDifference ICS, .

2D- :

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

Android: Howto clipRect API15

+1

Canvas; View. View ?

Canvas , invalidate() .

0

All Articles