在矩形外部填充画布

发布于 2024-10-17 18:46:04 字数 170 浏览 1 评论 0原文

我想填充画布上矩形之外的区域。我用来

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

绘制矩形,但不知道如何填充矩形/剪辑之外。

谢谢 杰夫

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

平安喜乐 2024-10-24 18:46:04

谢谢 ted 和 trojanfoe - 我想出的最好的解决方案是

    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);

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);
鹿港小镇 2024-10-24 18:46:04

你不会在剪辑之外填充;这就是剪辑要防止的事情!如果要填充矩形外部和绘图层边界内部的空间,请构造四个辅助矩形:

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());

然后填充它们。

You aren't going to fill outside the clip; that's the kind of thing clip is there to prevent! If you want to fill the space outside a rect and inside the drawing layer bounds, construct four auxiliary 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());

Then fill these.

萌逼全场 2024-10-24 18:46:04

ICS及以上...

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

XOR、Difference 和 ReverseDifference 剪辑模式是
如果启用硬件加速,则被 ICS 忽略。

只需在您的视图中禁用 2D 硬件加速即可:

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

参考Android:如何在API15中使用clipRect

ICS and above ...

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

XOR, Difference and ReverseDifference clip modes are
ignored by ICS if hardware acceleration is enabled.

Just disable 2D hardware acceleration in your view:

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

Reference Android: Howto use clipRect in API15

—━☆沉默づ 2024-10-24 18:46:04

您无法在 Canvas 之外进行绘制;该区域属于父View。您是否有能力子类化父 View 并在该类中进行绘图?

如果您想在 Canvas 剪辑之外进行绘制,则必须 invalidate() 您感兴趣的区域。

You can't draw outside of the Canvas; that area belongs to the parent View. Do you have the ability to subclass the parent View and do your drawing in that class instead?

If you want to draw outside of the Canvas clip then you'll have to invalidate() the areas you are interested in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文