Reference resources: https://blog.csdn.net/harvic880925/article/details/38875149
Drawing Chapter of Custom Controls (3): Range
1. Structural Region
1. Basic constructors (the third and fourth are commonly used)
public Region()// Create an empty region
Public Region // Copy the scope of a region
Public Region (Rect) // Create a rectangular region
public Region(int left, int top, int right, int bottom)// Create a rectangular region
2. Indirect structure
SetEmpty (): In a sense, vacancy is also a constructor, that is, to change an original regional variable into an empty variable, and to reconstruct the region using other Set methods.
set(Region region): Replace the original region with a new region value
set(Rect r): Replace the original area with the area represented by the rectangle
set(int left, int top, int right, int bottom): Similarly, a rectangular region is constructed from two points of the rectangle to replace the original value of the region.
setPath(Path path, Region clip): Construct a new region based on the intersection of the region of the path and a region
For example:
Region rgn = new Region(10,10,100,100);
//rgn.set(100, 100, 200, 200);
drawRegion(canvas, rgn, paint);
private void drawRegion(Canvas canvas,Region rgn,Paint paint)
{
RegionIterator iter = new RegionIterator(rgn);
Rect r = new Rect();
while (iter.next(r)) { canvas.drawRect(r, paint); } } 3,Use SetPath()Tectonic irregular region boolean setPath (Path path, Region clip) //Parameters: Path path: The path of the area to be constructed Region clip: In front of path The path is intersected and the intersection is set to the final region. //For example: //Constructing an elliptic path Path ovalPath = new Path(); RectF rect = new RectF(50, 50, 200, 500); ovalPath.addOval(rect, Direction.CCW); //When SetPath, a rectangular area smaller than an elliptical area is passed in and intersected. Region rgn = new Region(); rgn.setPath(ovalPath,new Region(50, 50, 200, 200)); //Draw the path drawRegion(canvas, rgn, paint); //2. Rectangular Set Enumeration Area-RegionIterator Class: It realizes the function of obtaining the rectangular set of the constituent region. RegionIterator(Region region) //Constructing Corresponding Rectangular Sets Based on Region boolean next(Rect r) //Get the next rectangle and save the result in the parameter Rect r //For example: private void drawRegion(Canvas canvas,Region rgn,Paint paint) { RegionIterator iter = new RegionIterator(rgn); Rect r = new Rect(); while (iter.next(r)) { canvas.drawRect(r, paint); } } //3. Regional merger, cross-over and other operations //Except that Union(Rect r) is the specified merge operation, the other four op () constructors are all the operations specified with another region. public final boolean union(Rect r) public boolean op(Rect r, Op op) { public boolean op(int left, int top, int right, int bottom, Op op) public boolean op(Region region, Op op) public boolean op(Rect rect, Region region, Op op) Op There are several types: DIFFERENCE //The final region is region 1 and region 2. INTERSECT //The final region is the region where region1 intersects region2. UNION //The final region is the region that combines region1 and region2. XOR //The final region is the region beyond the intersection of region 1 and region 2. REVERSE_DIFFERENCE //The final region is region 2 and region 1. REPLACE //The final region is region 2. //For example, take the intersection of two regions //Construct two rectangles Rect rect1 = new Rect(100,100,400,200); Rect rect2 = new Rect(200,0,300,300); //Construct a brush to draw a rectangular outline Paint paint = new Paint(); paint.setColor(Color.RED); paint.setStyle(Style.STROKE); paint.setStrokeWidth(2); canvas.drawRect(rect1, paint); canvas.drawRect(rect2, paint); //Construct two Region s Region region = new Region(rect1); Region region2= new Region(rect2); //Take the intersection of two regions region.op(region2, Op.INTERSECT); //Construct a brush to fill in the Region operation result Paint paint_fill = new Paint(); paint_fill.setColor(Color.GREEN); paint_fill.setStyle(Style.FILL); drawRegion(canvas, region, paint_fill); } private void drawRegion(Canvas canvas,Region rgn,Paint paint) { RegionIterator iter = new RegionIterator(rgn); Rect r = new Rect(); while (iter.next(r)) { canvas.drawRect(r, paint); } } //IV. Other Methods /**Several Judgment Methods*/ public native boolean isEmpty();//Judging whether the area is empty public native boolean isRect(); //Is it a matrix? public native boolean isComplex();//Is it a combination of multiple matrices? /**A series of getBound methods that return a Region boundary*/ public Rect getBounds() public boolean getBounds(Rect r) public Path getBoundaryPath() public boolean getBoundaryPath(Path path) /**A series of judgments about whether a point is included or intersected*/ public native boolean contains(int x, int y);//Does it contain a point? public boolean quickContains(Rect r) //Does it contain a rectangle? public native boolean quickContains(int left, int top, int right, int bottom) //Does it not contain a matrix? public boolean quickReject(Rect r) //Does it not intersect the rectangle? public native boolean quickReject(int left, int top, int right, int bottom); //Does it not intersect the rectangle? public native boolean quickReject(Region rgn); //Does it not intersect the rectangle? /**Several methods of translation transformation*/ public void translate(int dx, int dy) public native void translate(int dx, int dy, Region dst); public void scale(float scale) //hide public native void scale(float scale, Region dst);//hide
Drawing Chapter of Custom Controls (4): canvas Transform and Operation
Translating: Translating function is equivalent to translating coordinate system.
void translate(float dx, float dy)
Parameters:
float dx: the distance of horizontal translation, the amount of positive (right) translation, and the amount of negative (left) translation.
Flaotdy: The distance of translation in the vertical direction, the amount of translation in the positive direction (downward) and the amount of translation in the negative direction (upward).
II. The relationship between screen display and Canvas For example: // Construct two brushes, one red and one green. Paint paint_green = new Paint(); Paint paint_red = new Paint(); // Construct a rectangle Rect rect1 = new Rect(0,0,400,220); // Draw the border in green before translating the canvas canvas.drawRect(rect1, paint_green); // After translating the canvas, redraw the rectangle with a red border canvas.translate(100, 100); canvas.drawRect(rect1, paint_red);