Path related methods explained (I.)

Source: Internet
Author: User


The previous article focused on the canvas's translate (PAN), scale (zoom), rotate (rotation), skew (error-cutting), the next few are mainly on the Android path (encapsulated Bezier curve ) & DrawPath (path,paint) in the canvas;

Many people hear the Bezier curve, it seems to be very high-end atmosphere on the grade, the back will be with you to uncover its veil, a glimpse of tolerance distortion;

Path:

Let's look at some of the ways in the path class first.



Let's look down from the top:

There are two constructors, each of which is a

    /**     * Create an empty path     *    /public path () {        Mnativepath = init1 ();        Mdetectsimplepaths = Hardwarerenderer.isavailable ();    }
and the

    /**     * Create A new path, copying the contents from the SRC path.     *     * @param src the path to copy from when initializing the new path     *    /public path (path src) {        int Valnati ve = 0;        if (src! = null) {            valnative = Src.mnativepath;        }        Mnativepath = Init2 (valnative);        Mdetectsimplepaths = Hardwarerenderer.isavailable ();    }

There is nothing to say, the second is to directly reuse the attributes set in SRC to create a new path object;

Path.reset (): Clears the line and curve in path, but does not change its fill-type (back setfilltype);

Path.rewind (): clears the lines and curves in the path, but retains the internal data structure for reuse;

Path.set (path src); Replace the contents of the original path with the SRC content, and look at a small example:

Create a path, add a solid circle to the path

Mendpath = new Path (); Mendpath.addcircle (+, +, DIRECTION.CW);
Draw the path:

Canvas.drawpath (Mendpath, mpaint);
the effect is as follows:



Now add a rectangle to path:

Mendpath = new Path (); Mendpath.addcircle (+, +, DIRECTION.CW) mendpath.addrect (new RECTF (n, N, N), Direc tion. CW);
The effect is as follows:



Make the following changes:

Mendpath = new Path (); Mendpath.addcircle (+, +, DIRECTION.CW),//mendpath.addrect (new RECTF (+,-), Dir Ection. CW); Msrcpath = new Path (); Msrcpath.addrect (new RECTF (), DIRECTION.CW); Mendpath.set (Msrcpath);
run directly, if on more than 4.0 of the machine (4.0 and above hardware acceleration by default), you will find nothing on the screen, indicating that the method will be affected by hardware acceleration, turn off hardware acceleration, and then look at the effect:



Let's take a look at the Filltype-fill mode of path:

There are four types of Filltype defined in Android, namely:

Winding (0),

even_odd (1),

inverse_winding (2),

inverse_even_odd (3)

A graph can be used to illustrate the differences between the four modes:


To show that it is already very clear, let's test it with the following code:

Mendpath = new Path (), Mendpath.addcircle (DIRECTION.CW), Mendpath.addcircle (380, 380, DIRECTION.CW); Mendpath.setfilltype (filltype.inverse_even_odd); mpaint = new Paint (paint.anti_alias_flag); Mpaint.setstyle ( Style.fill); Mpaint.setcolor (color.red);
test results such as:


Do not set Filltype:

Setfilltype (filltype.winding) Setfilltype (filltype. even_odd ): 


           

Setfilltype (filltype.inverse_winding): Setfilltype (filltype. inverse_even_odd ):


According to this, path's filltype can be summarized as follows:

The default filltype of 1.Path is filltype.winding;

2. The scope of the function is to draw the whole Canvas of path, not the region of path;

3.filltype.winding: Take the path in all areas;

4.Filltype. even_odd: Take path does not intersect the area;

5.Filltype. inverse_winding: Take path to all areas not occupied;

6.Filltype. inverse_even_odd: Take path not occupied or intersect area;

Let's look at several methods related to filling patterns:

Getfilltype (): Do not say more, return to the fill mode of Path;

Setfilltype (): Sets the fill mode for Path;

Isinversefilltype (): Is the inverse fill mode:

Winding and even_odd return false,inverse_winding and inverse_even_odd return true;

Toggleinversefilltype (): Toggle the opposite fill mode, for example:

        Mendpath = new Path ();        Mendpath.addcircle (+, DIRECTION.CW);        Mendpath.addcircle (380, 380, DIRECTION.CW);        Mendpath.setfilltype (filltype.winding);        Mendpath.toggleinversefilltype ();        Mpaint = new Paint (paint.anti_alias_flag);        Mpaint.setstyle (Style.fill);        Mpaint.setcolor (color.red);
at this point the path is set to the winding fill mode, call Toggleinversefilltype (), the final mode is:

Filltype. inverse_winding

IsEmpty ():p ath is empty and returns true if path does not contain any lines and curves, otherwise false;

Isrect (RECTF Rect): Returns True if path specifies a rect, otherwise returns false, if True & Rect is not NULL, the RECT is set to the region of path;

Computebounds (RECTF bounds,boolean exact): Calculates the region of path and writes the result to bounds if the entire path contains only 0 or 1 points, it will return (0,0,0,0):

Use the following code to do the following test:

        Mcomputerect = new RECTF ();        Mendpath = new Path ();        Mendpath.addcircle (380, 380, DIRECTION.CW);        Mendpath.addrect (New RECTF (), DIRECTION.CW);        Mendpath.computebounds (Mcomputerect, false);        Toast.maketext (                mcontext, "                " + Mcomputerect.left + "," + Mcomputerect.top + "," + Mcomputerect.right + "," +                        m Computerect.bottom,                Toast.length_long). Show ();

Returns the result as (200,230,530,530), which is the bounding area of the content contained in path


Increserve (int extraptcount): indicates that path will increase the extraptcount point, which will enable path to allocate its storage space efficiently;


Well, this article mainly introduces these methods, the following main introduction path in Xxxto and addxxx related methods, and finally we will use path to do an example!














Path related methods explained (I.)

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.