[Book Note: Android game programming starts from scratch] 15. Game Development BASICS (cut area), learn android from scratch
A cut area is also called a visible area and is set by the canvas. It refers to setting an area on the canvas. Once a visible area is set on the canvas, NO content can be drawn. The visible area can be a circle, a rectangle, and so on.
The canvas provides three ways to set the visible area.
1. Set the visible area of the rectangle through coordinates
ClipRect (int left, int top, int right, int bottom)
Purpose: Set a rectangular visible area for the canvas.
Parameters 1 and 2: the upper left corner of the visible area
Third, four parameters: bottom right corner of the visible area
2. Use Path to set the shape of the visible area
ClipPath (Path path)
Purpose: Set a visible area for the canvas.
Parameter: Path instance
3. Use Region to set a visible area for the canvas
ClipRegion (Region region)
Purpose: Set a visible area for the canvas.
Parameter: Region instance
Region indicates a collection of regions, so it can set multiple Region blocks and solve some problems through the relationship between these Region blocks; for example, Region sets whether the Region where all the Region blocks intersect is visible, and sets the intersection area to show only the intersection.
Region common functions:
Op (Rect rect, Op op)
Purpose: set the region block.
First parameter: Rect instance
The second parameter: static value of Region. Op, indicating the display mode of the Region block. The area block is displayed as follows:
Region. Op. UNION: show all regions
Region. Op. INTERSECT: Area intersection display
Region. Op. XOR: The intersection area is not displayed.
The following is an example:
Create a new project with the game framework SurfaceView. For detailed steps, refer to "11. Game Development BASICS (differences between SurfaceView game frameworks, views, and SurfaceView )".
Modify the plotting function in the MySurfaceView class as follows:
Private void myDraw () {try {canvas = sfh. lockCanvas (); if (canvas! = Null) {// use image resources to generate a Bitmap bmp = BitmapFactory. decodeResource (this. getResources (), R. drawable. pic01); canvas. save (); canvas. drawText ("source image:", 20, 20, paint); canvas. drawBitmap (bmp, 20, 30, paint); canvas. restore (); canvas. save (); canvas. drawText ("using coordinates to set the visible area of a rectangle:", 20,320, paint ); /*** set the first and second parameters of the rectangle visible area for the canvas * the upper left corner of the visible area * The third and fourth parameters are the lower right corner of the visible area */canvas. clipRect (20,330, bmp. getWidth () + 20, bmp. getHeig Ht ()/2 + 330); canvas. drawBitmap (bmp, 20,330, paint); canvas. restore (); canvas. save (); canvas. drawText ("use Path to set the shape of the visible area, which is a circular visible area:", 20,620, paint); Path path = new Path (); path. addCircle (20 + bmp. getWidth ()/2,630 + bmp. getHeight ()/2, bmp. getWidth ()/2, Direction. CCW);/*** set the visible area for the canvas * parameter: The Path instance * uses Paht to set any required visible area for the bitmap. Here, a circular visible area is set. */Canvas. clipPath (path); canvas. drawBitmap (bmp, 20,630, paint); canvas. restore (); canvas. save (); canvas. drawText ("use Region to set the visible area for the canvas:", 20,920, paint); Region region = new Region (); // The area block displays all region. op (new Rect (20,930,120,103 0), Region. op. UNION); // do not display the Intersection region. op (new Rect (50,930,100,108 0), Region. op. XOR); canvas. clipRegion (region); canvas. drawBitmap (bmp, 20,930, paint); canvas. restore ();} Catch (Exception e) {}finally {if (canvas! = Null) {sfh. unlockCanvasAndPost (canvas );}}}