不太了解Region class

Do not understand Region class very well

我使用 xamarin 开发了一个 android 应用程序。当我尝试使用区域 class 时遇到问题,下面的代码可以正常工作并显示预期的蓝色区域。

Rect rect1 = new Rect(0, 0, 10,10);
canvas.ClipRect(rect1);
Rect rect2 = new Rect(20, 20, 30,30);
canvas.ClipRect(rect2, Region.Op.Union);
canvas.DrawRect(0, 0, 40, 40, paint);

但是下面的代码不起作用,什么都不显示,空白的背景

Region region = new Region();
Rect rect1 = new Rect(0, 0, 10,10);
region.Union(rect1);
Rect rect2 = new Rect(20, 20, 30,30);
region.Union(rect2);

canvas.ClipRegion(region);
canvas.DrawRect(0, 0, 40, 40, paint);

不知道为什么。

其实是可以的,但是可能被你的ActionBar或者其他什么东西挡住了,所以你看不到它,你认为它不起作用,比如this。但是如果你修改坐标,你可以看到你绘制的矩形。这是我的代码和结果:

Region region = new Region();
Rect rect1 = new Rect(200, 200, 300, 300);
region.Union(rect1);
Rect rect2 = new Rect(400, 400, 500, 500);
region.Union(rect2);

下面是document解释canvas.ClipRegion(region)方法:

Intersect the current clip with the specified region. Note that unlike clipRect() and clipPath() which transform their arguments by the current matrix, clipRegion() assumes its argument is already in the coordinate system of the current layer's bitmap, and so not transformation is performed.

编辑:

clipRect(), coornadite for clipRegion() 的坐标。