/** Create an empty region
*/
public Region() {
this(nativeConstructor());
}
/** Return a copy of the specified region
*/
public Region(Region region) {
this(nativeConstructor());
nativeSetRegion(mNativeRegion, region.mNativeRegion);
}
/** Return a region set to the specified rectangle
*/
public Region(Rect r) {
mNativeRegion = nativeConstructor();
nativeSetRect(mNativeRegion, r.left, r.top, r.right, r.bottom);
}
/** Return a region set to the specified rectangle
*/
public Region(int left, int top, int right, int bottom) {
mNativeRegion = nativeConstructor();
nativeSetRect(mNativeRegion, left, top, right, bottom);
}
Region需要和其他Rect、Region、Path、区域结合使用才能发挥效果,拥有下列几种方法
代码语言:javascript复制
/** Set the region to the specified region.
*/
public boolean set(Region region) {
nativeSetRegion(mNativeRegion, region.mNativeRegion);
return true;
}
/** Set the region to the specified rectangle
*/
public boolean set(Rect r) {
return nativeSetRect(mNativeRegion, r.left, r.top, r.right, r.bottom);
}
/** Set the region to the specified rectangle
*/
public boolean set(int left, int top, int right, int bottom) {
return nativeSetRect(mNativeRegion, left, top, right, bottom);
}
/**
* Set the region to the area described by the path and clip.
* Return true if the resulting region is non-empty. This produces a region
* that is identical to the pixels that would be drawn by the path
* (with no antialiasing).
*/
public boolean setPath(Path path, Region clip) {
return nativeSetPath(mNativeRegion, path.readOnlyNI(), clip.mNativeRegion);
}
/**
* 演示Region的View
*/
public class RegionView extends View {
private Paint mPaint = new Paint();
public RegionView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
Rect rect = new Rect(50, 50, 550, 550);
Region region = new Region(rect);
Path path = new Path();
path.addRoundRect(new RectF(rect),300,300, Path.Direction.CCW);
//将圆形和正方形区域结合
region.setPath(path,region);
//结合区域迭代器使用(得到图形里面的所有的矩形区域)
RegionIterator iterator = new RegionIterator(region);
Rect rect1 = new Rect();
while (iterator.next(rect1)) {
canvas.drawRect(rect1, mPaint);
}
}
}
发现和普通的画圆没什么区别,我们将paint的Style改成stroke
代码语言:javascript复制
mPaint.setStyle(Paint.Style.STROKE);
可以发现一个个矩形,Region用于复杂图形的实现,需要用到图形和图形的叠加,还提供了op()方法
代码语言:javascript复制
/**
* Perform the specified Op on this region and the specified rect. Return
* true if the result of the op is not empty.
*/
public boolean op(Rect r, Op op) {
return nativeOp(mNativeRegion, r.left, r.top, r.right, r.bottom,
op.nativeInt);
}
/**
* Perform the specified Op on this region and the specified rect. Return
* true if the result of the op is not empty.
*/
public boolean op(int left, int top, int right, int bottom, Op op) {
return nativeOp(mNativeRegion, left, top, right, bottom,
op.nativeInt);
}
/**
* Perform the specified Op on this region and the specified region. Return
* true if the result of the op is not empty.
*/
public boolean op(Region region, Op op) {
return op(this, region, op);
}
/**
* Set this region to the result of performing the Op on the specified rect
* and region. Return true if the result is not empty.
*/
public boolean op(Rect rect, Region region, Op op) {
return nativeOp(mNativeRegion, rect, region.mNativeRegion,
op.nativeInt);
}
/**
* Set this region to the result of performing the Op on the specified
* regions. Return true if the result is not empty.
*/
public boolean op(Region region1, Region region2, Op op) {
return nativeOp(mNativeRegion, region1.mNativeRegion,
region2.mNativeRegion, op.nativeInt);
}
其中,我们需要了解的是Op的几种模式
代码语言:javascript复制
public enum Op {
DIFFERENCE(0),
INTERSECT(1),
UNION(2),
XOR(3),
REVERSE_DIFFERENCE(4),
REPLACE(5);
Op(int nativeInt) {
this.nativeInt = nativeInt;
}
/**
* @hide
*/
public final int nativeInt;
}