Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill canvas outside rectangle

Tags:

android

canvas

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

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

to draw the rectangle, but can't figure out how to fill outside the rectangle/clip.

Thanks Geoff

like image 213
Geoff Avatar asked Feb 14 '11 23:02

Geoff


1 Answers

Thanks ted and trojanfoe - the neatest solution I've come 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);
like image 78
Geoff Avatar answered Nov 05 '22 05:11

Geoff