Android ClipRect op.xxx each parameter understanding

Source: Internet
Author: User

A little wordy of a study note, you can directly see the final conclusion: the various pictures and descriptions in front of the final conclusion of the service

1) Cut: As with the normal drawing tool cut, cut an area on the canvas, such as cut a rect area, throw away all the rest of the canvas, and all the drawing is done within the Rect area (if the save and restore methods are not involved).

2) clip does not affect the previously drawn graphics when it is clipped.

3) Clipping is the operation of the canvas, not the diagram on the canvas.

To illustrate these points, let's take a look at the following.


Step 1) Draw a full-screen blue rectangle before clipping.

2) Define the clipped rectangular rect area and cut

3) Set the canvas color to red after clipping

The effect of the run is as follows: The area of the canvas after clipping is the clipped rect area, although the top and bottom parts of the canvas are clipped, but the blue part shown on the way is still displayed without clipping: As stated above, it will not affect the previously drawn graphics.

The code is as follows:

protected void OnDraw (canvas canvas) {int width = getmeasuredwidth (); int height = getmeasuredheight (); Paint paint = new paint ();p aint.setcolor (Color.Blue);//Full screen drawing of a blue rectangular picture canvas.drawrect (0, 0,width,height,paint);// Defines the clipped rect area of the rect rect = new rect (); rect.left = 0;rect.top = 300;//upper left corner (0,300) rect.right = Width;rect.bottom = HEIGHT/2; Lower right Corner (width,height)//Perform cut          canvas.cliprect (rect)    Canvas.drawcolor (color.red);}

The above cliprect is only one parameter (the default is intersect, see its overloaded method Cliprect (rect,op.xx) The second parameter and the meaning of each parameter and the effect of the operation:

Do some preparatory work before you begin:

Area A: The canvas area before clipping

Area B: The area that is currently being clipped, that is, the area represented by the first parameter of Cliprect


The translation and lookup of these fields before the code test is not directly written and the following assumptions are made about the various fields in the OP.XXX:

XOR: The difference or operation, the same as 0 (false), not 1 (true), that is, two areas overlap The place is 0, the different place is 1, the area of the clipped canvas is the portion of two areas that do not overlap.

INTERSECT: Intersection operation, which is the area of intersection of two regions after the cut-off area of the canvas.

Replace: This is to be verified, unknown so, literally means to replace, it should be the area of B instead of a area, so the canvas area after the cut may be only the area of B, of course, this is only a hypothesis, the following verification.

Difference: Difference set operation, the area of the cropped canvas is a-b

Reverse_difference: Difference set operation, REVERSE to reverse, reverse meaning, it is not difficult to guess the area of the canvas after the cut is b-a

UNION: The combined operation, after which the area of the canvas is added to the area of A and B, and of course the overlapping places are counted only once

The following will write the program attention to verify the above results and draw the corresponding conclusions

First complete and subset: At this point A is the complete area and B is a subset of a

A) Op.difference: The difference between the area to be clipped (set B) and the current canvas (set a), and the canvas canvas area remaining after the cut is the area of a-B to see the results of the run

Step or previous step, just change canvas.cliprect (rect) to Canvas.cliprect (rect,op.difference); The result is as follows:


b) Op.reverse_difference: Comparing difference takes a-b,reverse is the opposite meaning, so for b-a, because B is a subset of a, so the difference set does not exist, so the result is the following:


c) Op.xor: Two regions for the XOR operation, the rule of XOR is: "The same is 0 (false), the difference is 1 (true)", when the two regions are different or, it is clear that "area overlap of 0, non-overlapping place is 1", The area of the canvas that is left over after clipping is the area of the non-overlapping areas where you see the results of the operation


D) op.union the set of A and B, so the result of the operation is:

e) Op.intersect the intersection of a and B, because the above program A is a full-screen canvas range, and b is inside a, so the result of the cut is shown, so the intersection is the range of B

f) Op.replace, the operating effect is as follows

With the results of the complete and subset operation, replace should be the same as the one above, and the rest is the same as the idea.

So I tested the non-complete and subset cases.

At this point the relationship between Zone A and B, where c is the intersection area of A and B:

To test the drawing steps:

1) draw a blue rectangular face

2) Define a Rect object (the area to be clipped for the first time) and start cutting

3) Define the Rect object of B and start clipping, and call Cliprect (RECT,OP.XXX) to see the effect of clipping without xxx

4) Set the cut-off canvas to red

The code for the above steps is as follows:

                int width = 1000;         int height =1000;                Paint paint = new paint ();p aint.setcolor (Color.Blue);//Full screen drawing of a blue rectangular picture canvas.drawrect (0, 0,width,height,paint);              A Rect object that defines the zone a rect rect = new rect (); Rect.left =100;rect.top = 100;//upper left corner (0,300) rect.right = 300;rect.bottom = 300; The lower right corner (width,hei//clipping is a zone canvas.cliprect (rect);/                /Rect object with ClipRect//set b rect.left =200;rect.top = 200;//upper left corner ( 0,300) rect.right = 400;rect.bottom = 400; Lower right Corner (width,height) canvas.cliprect (rect,op.xxxx);                Set the cropped canvas to Red                canvas.drawcolor (color.red);

A) operating effect in case of difference (a difference set of a-B): That is, the area of zone C is removed

b) Reverse_difference effect such as (b-a difference): That is, the area B removed C area


c) The result of the UNION's operation (A and B unions)

d) INTERSECT (intersection operation) at this point the canvas is left with only the C area to display, so the results are as follows


e) XOR operation, the result of the cut is a and B areas do not overlap

f) REPLACE What is the result of this operation? is the B area, that is, the result of the cut retains only the B area, which is the meaning of replace replace or replace: Area A that is not currently clipped is replaced by the area B that is about to be cut.


Conclusion:

XOR: The difference or operation, the same as 0 (false), not 1 (true), that is, two areas overlap The place is 0, the different place is 1, the area of the clipped canvas is the portion of two areas that do not overlap.

INTERSECT: Intersection operation, which is the area of intersection of two regions after the cut-off area of the canvas.

Difference: Difference set operation, the area of the cropped canvas is a-b

Reverse_difference: Difference set operation, REVERSE to reverse, reverse meaning, it is not difficult to guess the area of the canvas after the cut is b-a

UNION: The a+b area of the canvas after clipping

Replace: That is, the result of the cut retains only the B area, which is the meaning of replace or replace: Area A that is not currently clipped is replaced by the area B that is about to be clipped.



Android ClipRect op.xxx each parameter understanding

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.