一.Region
1.直接构造
public Region()
public Region(Region region)
public Region(Rect r)
举例
public class BasisView extends View {
public BasisView(Context context) {
super(context);
}
public BasisView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public BasisView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(5);
Rect rect = new Rect(50, 50, 200, 100);
Region region = new Region(rect);
drawRegion(canvas, paint, region);
}
private void drawRegion(Canvas canvas, Paint paint, Region region) {
RegionIterator regionIterator = new RegionIterator(region);
Rect rect = new Rect();
while (regionIterator.next(rect)) {
canvas.drawRect(rect, paint);
}
}
}
2.间接构造
set系列函数
public void setEmpty()
public boolean set(Region region)
public boolean set(Rect r)
public boolean set(int left, int top, int right, int bottom)
public boolean setPath(Path path, Region clip)
利用setPath构造不规则区域
- path:地址
- clip:与前面的path所构成的路径取交集
举例:
public class BasisView extends View {
public BasisView(Context context) {
super(context);
}
public BasisView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public BasisView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(5);
Path ovalPath = new Path();
RectF rectF = new RectF(50, 50, 200, 500);
ovalPath.addOval(rectF, Path.Direction.CCW);
Region region = new Region();
region.setPath(ovalPath,new Region(50, 50, 200, 200));
drawRegion(canvas, paint, region);
}
private void drawRegion(Canvas canvas, Paint paint, Region region) {
RegionIterator regionIterator = new RegionIterator(region);
Rect rect = new Rect();
while (regionIterator.next(rect)) {
canvas.drawRect(rect, paint);
}
}
}
3.枚举区域-RegionIterator
对于特定区域,可以使用多个矩形表示。
canvas没有直接绘制区域的方法,所以需要RegionIterator来实现。
private void drawRegion(Canvas canvas, Paint paint, Region region) {
RegionIterator regionIterator = new RegionIterator(region);
Rect rect = new Rect();
while (regionIterator.next(rect)) {
canvas.drawRect(rect, paint);
}
}
4.区域相交
boolean union(Rect r)
举例
Region region = new Region(10,10,200,100);
region.union(new Rect(50, 50, 200, 200));
drawRegion(canvas, paint, region);
系列参数
boolean op(Rect r, Region.Op op)
boolean op(int left, int top, int right, int bottom,Region.Op op)
boolean op(Region region, Region.Op op)
op是枚举类型,有六个参数值
public static enum Op {
DIFFERENCE,
INTERSECT,
UNION,
XOR,
REVERSE_DIFFERENCE,
REPLACE;
private Op() {
}
}
- DIFFERENCE:region1与region2不同的区域
- INTERSECT:region1与region2相交的区域
- UNION:region1与region2组合的区域
- XOR:region1与region2相交之外的区域
- REVERSE_DIFFERENCE:region1与region2不同的区域
- REPLACE:region2的区域
举例
public class BasisView extends View {
public BasisView(Context context) {
super(context);
}
public BasisView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public BasisView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(5);
Rect rect1 = new Rect(100, 100, 400, 200);
Rect rect2 = new Rect(200, 0, 300, 300);
Region region1 = new Region(rect1);
Region region2 = new Region(rect2);
region1.op(region2, Region.Op.DIFFERENCE);
drawRegion(canvas,paint,region1);
}
private void drawRegion(Canvas canvas, Paint paint, Region region) {
RegionIterator regionIterator = new RegionIterator(region);
Rect rect = new Rect();
while (regionIterator.next(rect)) {
canvas.drawRect(rect, paint);
}
}
}
4.其他函数
判空
public native boolean isEmpty();
判断是否是矩形
public native boolean isRect();
判断是否是多矩形组合
public native boolean isComplex();
getBound系列
//获得矩形边界
public Rect getBounds()
public boolean getBounds(@NonNull Rect r)
//返回矩形对应的path
public Path getBoundaryPath()
public boolean getBoundaryPath(Path path)
是否包含某个点
public native boolean contains(int var1, int var2);
是否包含某个矩形
public boolean quickContains(Rect r)
是否相交
public boolean quickReject(Rect r)
public native boolean quickReject(Region var1)
平移变换
public void translate(int dx, int dy)
二.Canvas
1.平移
public void translate(float dx, float dy)
举例
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(5);
Rect rect1 = new Rect(0, 0, 400, 200);
canvas.translate(100,200);
canvas.drawRect(rect1, paint);
}
2.旋转
public void rotate(float degrees)
public final void rotate(float degrees, float px, float py)
- degrees:旋转的度数,正数顺时针,负数逆时针。
- px:旋转的中心点X坐标。
- py:旋转的中心点Y坐标。
3.缩放
public void scale(float sx, float sy)
public final void scale(float sx, float sy, float px, float py)
4.扭曲
public void skew(float sx, float sy)
5.裁剪画布(clip系列函数)
public boolean clipRect(RectF rect, Op op)
public boolean clipRect(Rect rect, Op op)
public boolean clipRect(RectF rect)
public boolean clipOutRect(RectF rect)
public boolean clipRect(Rect rect)
public boolean clipOutRect(Rect rect)
public boolean clipRect(float left, float top, float right, float bottom, Op op)
public boolean clipRect(float left, float top, float right, float bottom)
public boolean clipOutRect(float left, float top, float right, float bottom)
public boolean clipRect(int left, int top, int right, int bottom)
public boolean clipOutRect(int left, int top, int right, int bottom)
public boolean clipPath(Path path, Op op)
public boolean clipPath(Path path)
public boolean clipOutPath(Path path)
5.保存与恢复
public int save()
public void restore()
可进行多次保存与恢复
恢复特定的画布
public void restoreToCount(int saveCount)