填充画布外面的矩形

Geo*_*off 7 android canvas

我想填充画布上矩形外的区域.我用

 canvas.drawRect(pTopLeft.x, pTopLeft.y, pBotRight.x, pBotRight.y, paint);
Run Code Online (Sandbox Code Playgroud)

绘制矩形,但无法弄清楚如何填充矩形/剪辑外.

谢谢杰夫

Geo*_*off 18

谢谢泰德和特洛伊福福 - 我提出的最好的解决方案是

    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);
Run Code Online (Sandbox Code Playgroud)


Ted*_*opp 6

你不会在剪辑之外填写; 这就是夹子有防止的东西!如果要填充rect之外的空间和绘图层边界内部,请构造四个辅助rects:

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());
Run Code Online (Sandbox Code Playgroud)

然后填写这些.

  • 我知道你现在正在使用它,但另一种可能性发生在我身上:设置一个排除中间和填充的剪辑区域.我认为如果你使用`canvas.clipRect(innerRect,Region.Op.DIFFERENCE)`它会打出你想要的洞. (4认同)