Android clipRect Op. xxx parameter understanding, cliprectop. xxx

Source: Internet
Author: User

Android clipRect Op. xxx parameter understanding, cliprectop. xxx

A learning note that is a little too long, you can directly look at the final conclusion: the various pictures and instructions above serve the final conclusion.

1) cut: cut an area on the canvas, such as cutting a Rect area. The rest of the canvas is lost, then all the drawings are performed in this Rect area (if the save and restore methods are not involved ).

2) When clip is cut, it does not affect the previously drawn image.

3) The cut operation is performed on the canvas, not on the canvas.

To illustrate these points, we will explain them below.


Step 1) Draw a blue rectangle in full screen before cutting.

2) define the Rect area of the cut rectangle and cut it.

3) set the canvas color to red after cutting.

Shows the running effect: the area of the canvas after the cut is the Rect area of the cut, although the area above and below the canvas is cut out, however, the blue part shown on the way will still be displayed without being cut off: that is, the previously drawn image will not be affected as mentioned above.

The Code is as follows:

Protected void onDraw (Canvas canvas) {int width = getMeasuredWidth (); int height = getMeasuredHeight (); Paint paint = new Paint (); paint. setColor (Color. BLUE); // draw a BLUE rectangular canvas in full screen. drawRect (0, 0, width, height, paint); // defines the Rect area of the cut Rect rect = new Rect (); rect. left = 0; rect. top = 300; // the upper left corner (0,300) rect. right = width; rect. bottom = height/2; // bottom right corner (width, height) // cut the canvas. clipRect (rect) canvas. drawColor (Color. RED );}

The preceding clipRect has only one parameter (the default value is INTERSECT). Let's take a look at its reload method Cliprect (rect, OP. xx). The second parameter, its meaning and running effect are as follows:

Make preparations before you start:

Area A: Area of the canvas before cutting

Area B: the area to be cut, that is, the area indicated by the first parameter of clipRect.


Before directly writing code for testing, I made the following assumptions in my mind about the translation of these fields, reading and writing mathematical documents, and various fields of Op. xxx:

XOR: exclusive or operation. The same value is 0 (false). Do not set it to 1 (true). That is, the overlap between the two regions is 0, and the difference is 1, the area of the cut canvas is the area of the two areas that do not overlap.

INTERSECT: intersection operation. The canvas area after the cut is the intersection area of the two areas.

REPLACE: this is to be verified. It is not clear. So, literally, it means to REPLACE Area A with area B, therefore, the area of the canvas after the cut may be the area with only B left. Of course, this is just a hypothesis and is subject to the verification below.

DIFFERENCE: DIFFERENCE set operation, the area of the cut canvas is A-B

REVERSE_DIFFERENCE: difference set operation, REVERSE for REVERSE, REVERSE meaning, it is not difficult to guess after the cut canvas area is the area of the B-A

UNION: the UNION operation. After the cut, the canvas area is the sum of the areas A and B. Of course, the overlap is calculated only once.

Next, write a program to verify the above results and draw the corresponding conclusions.

First, the complete set and subset: At this time, A is the complete set area, and B is the subset of

A) Op. DIFFERENCE: the DIFFERENCE set between the area to be cut (Set B) and the current canvas (Set A). The canvas area that is left after the cut is the area of the A-B and look at the running result

The procedure is the previous step. You only need to change canvas. clipRect (rect) to canvas. clipRect (rect, Op. DIFFERENCE). The running result is as follows:


B) Op. REVERSE_DIFFERENCE: compare DIFFERENCE to take the A-B, REVERSE is the opposite meaning, so for the B-A, because B is the subset of A, so the DIFFERENCE set does not exist, so the running result is below:


C) Op. XOR: two regions are used for exclusive or operations. The rule of the exclusive or operation is: "The same is 0 (false), the difference is 1 (true)". When two regions are used for exclusive or operation, obviously, "the place where the area overlaps is 0 and the place where the area does not overlap is 1". After the cut, the area of the remaining canvas is the area where the area does not overlap. Check the running result.


D) Op. UNION takes the UNION of A and B, so the running result is:

 

E) Op. INTERSECT obtains the intersection of A and B, because in the above program, A is the full screen canvas range, and B is in A, so the cut result is shown, so the intersection is the range of B.

F) Op. REPLACE. The running effect is as follows:

From the running effect of the complete set and subset, REPLACE should be the same as above, while the rest is the same as the assumption.

So I tested non-Complete Sets and subsets.

In this case, the relationship between zone A and Zone B is C, and the intersection of Zone A and Zone B is:

Test drawing steps:

1) Draw a blue rectangular area

2) define the Rect object of A (the area to be cut for the first time) and start cutting

3) define the rect object of B and start cutting, and call clipRect (rect, Op. xxx) to see the effect of cutting without xxx.

4) set the cut canvas to red.

The above code is as follows:

Int width = 1000; int height = 1000; Paint paint = new Paint (); paint. setColor (Color. BLUE); // draw a BLUE rectangular canvas in full screen. drawRect (0, 0, width, height, paint); // defines the rect object Rect rect Rect = new rect (); rect. left = 100; rect. top = 100; // the upper left corner (0,300) rect. right = 300; rect. bottom = 300; // bottom right corner (width, hei // cut to the canvas of Area. clipRect (rect); // same as clipRect // set rect object rect of B. left = 200; rect. top = 200; // the upper left corner (0,300) rect. right = 400; rect. bottom = 400; // bottom right corner (width, height) canvas. clipRect (rect, Op. xxxx); // set the cropped canvas to a red canvas. drawColor (Color. RED );

A) DIFFERENCE in the case of running effect (the A-B of the DIFFERENCE set): that is, Zone a to Remove Area C

B) The Running Effect of REVERSE_DIFFERENCE, such as (the difference set of B-A): that is, Area B removes Area C.


C) running result of UNION (UNION of A and B)

D) INTERSECT (intersection operation) the canvas is only available in Area C, so the running result is as follows:


E) XOR exclusive or operation. After the cut, the result is A region that does not overlap with area B.

F) What is the result of the REPLACE operation? That is, the B region, that is, the cut result only retains the B region, which is the meaning of REPLACE replacement or replacement: region A that has not yet been cut is replaced by region B to be cut.


Conclusion:

XOR: exclusive or operation. The same value is 0 (false). Do not set it to 1 (true). That is, the overlap between the two regions is 0, and the difference is 1, the area of the cut canvas is the area of the two areas that do not overlap.

INTERSECT: intersection operation. The canvas area after the cut is the intersection area of the two areas.

DIFFERENCE: DIFFERENCE set operation, the area of the cut canvas is A-B

REVERSE_DIFFERENCE: difference set operation, REVERSE for REVERSE, REVERSE meaning, it is not difficult to guess after the cut canvas area is the area of the B-A

UNION: UNION operation. The area of the cut canvas is A + B.

REPLACE: that is, the cut result only retains region B, which is the meaning of REPLACE replacement or replacement: region A that has not yet been cut is replaced by region B to be cut.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.