Mainly to look this article study: http://blog.csdn.net/lonelyroamer/article/details/8349601
Region.op parameters
- difference (0), //final area region1 and Region2 different area
- intersect (1), // the final area is region1 with region2;
- union (2), // The final area is the area of the region1 and region2 combination;
- XOR (3), //the final area is an area outside the region1 - region2 intersection
- reverse_difference (4), //the final area is region2 and region1 different areas
- REPLACE (5) //the final area region2 for
Canvas Cropping related methods:
1, the most basic cliprect, cutting a rectangle
2, Clippath, clipping path includes the scope, path includes the scope is not empty to be effective.
3, Clipregion.
Public classCustomViewextendsView {Privatecontext Context; //brush variables, must be created PrivatePaint paint; Privatepath Path; PublicCustomView (Context context) {Super(context); This. Context =context; Initview (); } PublicCustomView (context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); This. Context =context; Initview (); } PublicCustomView (Context context, AttributeSet attrs) {Super(context, attrs); This. Context =context; Initview (); } Private voidInitview () {Paint=NewPaint (); //anti-aliasingPaint.setantialias (true); Paint.settextsize (6); //where to draw text//Paint.Align.CENTER//Paint.Align.LEFT//Paint.Align.LEFTpaint.settextalign (Paint.Align.RIGHT); Path=NewPath (); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); //Setting the canvas colorCanvas.drawcolor (Color.yellow); //Specific Explanation:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0304/960.html //Save : Used to save the state of the canvas. After save, you can invoke canvas panning, indenting, rotating, trimming, cropping, and so on. //Restore : The state used to restore the canvas before it is saved. Preventing the actions that are performed on the canvas after save has an impact on subsequent drawing. Canvas.save (); //Canvas for horizontal movementCanvas.translate (10, 10); Drawscene (canvas); Canvas.restore (); //Start crop Operation//Save the canvas state firstCanvas.save (); Canvas.translate (160, 10); Canvas.cliprect (20, 20, 100, 100); //region, Chinese meaning is the meaning of an area, which represents a block of closed area on a canvas layer. //refer to http://blog.csdn.net/lonelyroamer/article/details/8349601//Region.Op.INTERSECT//Region.Op.REPLACE//Region.Op.REVERSE_DIFFERENCE//Region.Op.UNION//Region.Op.XOR//Region.Op.DIFFERENCECanvas.cliprect (40, 40, 80, 80, Region.Op.DIFFERENCE); Drawscene (canvas); } Private voiddrawscene (canvas canvas) {//Draw Rectangle 0,0 upper left corner, 100,100 lower right cornerCanvas.cliprect (0, 0, 100, 100); //Canvas color is whiteCanvas.drawcolor (Color.White); //Brush is redPaint.setcolor (color.red); //draw a line from the upper-left corner of the rectangle to the lower-right corner of the rectangleCanvas.drawline (0, 0, 100, 100, paint); //brush color is greenPaint.setcolor (Color.green); //Draw Circle, center Point, 30, 70, RadiusCanvas.drawcircle (30, 70, 30, paint); //Brush is bluePaint.setcolor (Color.Blue); //draw text, text coordinates 100,30Canvas.drawtext ("Clipping", 100, 30, paint); }}
Effect:
Not detailed, the back
Canvas clipping and region, Regioniterator