"The zero start of Android game Programming" 15. Game Development Basics (clipping area)

Source: Internet
Author: User
Tags drawtext

The clipping area, also known as the viewable area, is set by the canvas, which refers to the setting of an area on the canvas that, when the canvas is set to a visible area, will not be visible except for this area, and the viewable area can be a circle, a rectangle, and so on.

The canvas provides three ways to set the viewable area.

1. Set the visible area of the rectangle by coordinates
ClipRect (int left,int top,int Right,int bottom)
Function: Set a rectangular viewable area for the canvas
First to second parameter: the upper-left corner of the viewable area
Third to fourth parameter: the lower-right corner of the viewable area

2. Use Path to set the shape of the visible area
Clippath (path Path)
Function: Set the visible area for the canvas
Parameters: path Instance

3. Use region to set the visible area of the canvas
Clipregion (region)
Function: Set the visible area for the canvas
Parameters: Region Instance

Region represents a collection of regions, so it can set up multiple area blocks, and it can handle some problems through the relationships between the blocks, such as region setting whether the area where all the blocks intersect is visible, setting the intersection area to show only the intersection, and so on.

Region Common functions:
OP (Rect rect,op op)
Function: Set area block
First parameter: instance of Rect
Second parameter: Region.op a static value that represents how the area block is displayed. The area blocks are displayed in the following way:
Region.Op.UNION: Area Show All
Region.Op.INTERSECT: Intersection of Regions display
Region.Op.XOR: Do not show intersection area


Here is an example demonstration, as follows:

Create new project, game frame for Surfaceview game frame. Refer to "11. Game Development Basics (Surfaceview game frame, View, and Surfaceview)."

Modify the drawing functions in the Mysurfaceview class as follows:

    Private voidMydraw () {Try{Canvas=Sfh.lockcanvas (); if(Canvas! =NULL) {                    //generate a bitmap bitmap from a picture resourceBitmap bmp = Bitmapfactory.decoderesource ( This. Getresources (), R.DRAWABLE.PIC01);                Canvas.save (); Canvas.drawtext ("Original:", 20, 20, paint); Canvas.drawbitmap (BMP,20, 30, paint);                                Canvas.restore ();                Canvas.save (); Canvas.drawtext ("Set the visible area of the rectangle through coordinates:", 20, 320, paint); /*** Set Rectangle visible area for canvas * The first to second parameter is the upper-left corner of the viewable area * The third to fourth parameter is the lower-right corner of the visible area /c5>*/Canvas.cliprect (20,330,bmp.getwidth () +20,bmp.getheight ()/2+330); Canvas.drawbitmap (BMP,20, 330, paint);                                Canvas.restore ();                Canvas.save (); Canvas.drawtext ("Use Path to set the shape of the viewable area, which is a circular viewable area:", 20, 620, paint); Path Path=NewPath (); Path.addcircle (20+bmp.getwidth ()/2, 630+bmp.getheight ()/2, Bmp.getwidth ()/2, DIRECTION.CCW); /*** Set the viewable area for the canvas * Parameters: path instance * Use PAHT to set any desired viewable area for the bitmap, here is a circular viewable area set. */Canvas.clippath (path); Canvas.drawbitmap (BMP,20, 630, paint);                                Canvas.restore ();                Canvas.save (); Canvas.drawtext ("Use region to set the visible area of the canvas:", 20, 920, paint); Region=NewRegion (); //area block Display allRegion.op (NewRect (20,930,120,1030), Region.Op.UNION); //do not show intersection areaRegion.op (NewRect (50,930,100,1080), Region.Op.XOR);                Canvas.clipregion (region); Canvas.drawbitmap (BMP,20, 930, paint);                Canvas.restore (); }        } Catch(Exception e) {}finally {            if(Canvas! =NULL) {sfh.unlockcanvasandpost (canvas); }        }    }

"The zero start of Android game Programming" 15. Game Development Basics (clipping area)

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.