Sixth chapter a piece of white paper good painting-canvas canvas (4)

Source: Internet
Author: User

6.4.4 Path Android.graphics.Path

When our demand is an irregular figure, the canvas of the DrawRect and other methods are not, here will use the DrawPath (path path, paint paint) method to draw a shape by path. Canvas also has a method Clippath (path Path). This method is used to set a valid area in the canvas according to the design path.

Here we introduce the following path class, which is a collection of multiple points and graphs.

The method of constructing path is relatively simple, as follows:

Path path1 = new Path (); Construction method

Below we draw a closed prototype path, we use the Addcircle method of the path class.

Path1.addcircle (10,10,50,DIRECTION.CW);

Explain this method:

Voidaddcircle (float x, float y, float radius, Direction dir)

The parameter x is the horizontal position of the x-axis, the parameter y is the vertical position of the y-axis, the radius of the radius is circular, the dir is the direction of the drawing, The CW is clockwise, and the CCW is counterclockwise.

Also we are free to add some dots and lines, and make a triangle.

Path path2 = new Path ();

Move the starting point of the path to 90,330

Path2.moveto (90, 330);

Draw a line from 90,330 to 150,330

Path2.lineto (150,330);

Draw a line from 150,330 to 120,270

Path2.lineto (120,270);

closes the current profile. so it's a triangular shape.

Path2.close ();

Combining the drawing methods in the Canvas class DrawPath () and Drawtextonpath (), we can add the following code to the OnDraw ().

Here Pathpaint the color of the brush for the path

Canvas.drawpath (path1, pathpaint);

Draw the text to the path

Canvas.drawtextonpath ("Android", path2,0,15, Textpaint);

Below, our OnDraw () method demonstrates how to draw a path.

@Override

protected void OnDraw (canvas canvas) {

Paint Pathpaint =new paint ();

Paint Textpaint =new paint ();

The path of the paint brush is red

Pathpaint.setcolor (color.red);

Set Paint's style to fill: solid

Pathpaint.setstyle (Paint.Style.FILL);

The text on the path is blue

Textpaint.setcolor (Color.Blue);

Path path1 = new Path ();

Path path2 = new Path ();

Omit part of the code

Canvas.drawpath (path1, pathpaint);

Draw text on a path

Canvas.drawtextonpath ("Android", path2,0,15, Textpaint);

}

Explain the following method:

Voiddrawtextonpath (String text, path path, float hoffset, float vOffset, Paintpaint)

The parameter text is the text content that needs to be drawn on the path, the parameter path, the path to which the text is drawn, the distance the parameter Hoffset from the path, the parameter voffset, the upper and lower height of the path, the float float, except for the precision of 8 decimal places, can be positive or negative, when the literal is in the circle of the path, negative when the loop outside the path; the parameter paint, and finally still a paint object used to make text color, font, size and other properties.

Other methods commonly used for path classes are shown in table 6-5.

Method

return value

Description

AddArc (RECTF oval, float startangle, float sweepAngle)

void

Add a polygon to a path

Addcircle (float x, float y, float radius, path.direction dir)

void

Add a circle to a path

Addoval (RECTF Oval, path.direction dir)

void

Add ellipse

Addrect (RECTF rect, path.direction dir)

void

Add a Zone

Addroundrect (RECTF rect, float[] radii, path.direction dir)

void

Add a fillet area

IsEmpty ()

Boolean

Determine if the path is empty

Transform (Matrix matrix)

void

Applying matrix transformations

Transform (Matrix matrix, Path DST)

void

Apply a matrix transformation and place the result in a new path, which is the second parameter.

Table 6-5 Other methods common to the Path class

Advanced effect of 6.4.5 path Android.graphics.PathEffect

Whether a straight line is too dull, let's look at the high-level effect of the path, as shown in 6-2. is not very flashy, these effects are actually implemented using the Patheffect class.

Figure 6-2 Advanced effects of paths

Patheffect is especially useful for drawing path basic graphics, which can be applied to any paint to affect how lines are drawn. With Patheffect, you can change the appearance of a corner of a shape and control the appearance of the contour. The Apidemos (Com.example.android.apis.graphics.PathEffects.java) example included with the SDK gives instructions on how to apply each effect. Figure 6-2 is the patheffects in Apidemos.

Android contains several patheffect, including:

1) Cornerpatheffect can use rounded corners instead of sharp corners to smooth the sharp corners of the base shape.

2) Dashpatheffect can use Dashpatheffect to create a dashed outline (dash/dot) instead of using solid lines. You can also specify a repeating pattern for any virtual/real segments.

3) Discretepatheffect is similar to dashpatheffect, but it adds randomness. When drawing it, you need to specify the length of each segment and the degree of deviation from the original path.

4) Pathdashpatheffect This effect can define a new shape (path) and use it as the outline marker for the original path.

A complex effect can use multiple path effect in a single paint to combine a patheffect.

5) Sumpatheffect adds two effects in a single path sequentially, so that each effect can be applied to the original path, and the two effects are combined. Sumpatheffect (First, second) = First (path) + second (path)

6) Composepatheffect combination of two effects, the result is to use the first effect, and then on the basis of this effect to apply the second effect. Composepatheffect (outer, inner) = outer (inner (path)).

7) Discretepatheffect divides the path into segments of a specified length and then randomly shifts each segment to its original position.

Changes in the patheffect of the object shape affect the area of the shape. This ensures that the fill effect applied to the same shape will be drawn to the new boundary.

The core code above is as follows:

phase specifies the actual and false offset on the dashed line, each add 1, the equivalent of the swap virtual place and the location.

This can be achieved by constantly refreshing the actual situation to change the effect of the animation to people.

private static void Makeeffects (patheffect[] e, float phase) {

E[0] = null;

E[1] = new Cornerpatheffect (10);

E[2] = new Dashpatheffect (new float[] {Ten, 5, 5, 5}, phase);

E[3] = new Pathdashpatheffect (Makepathdash (), Phase,

PathDashPathEffect.Style.TRANSLATE);

E[4] = new Pathdashpatheffect (Makepathdash (), Phase,

PathDashPathEffect.Style.ROTATE);

E[5] = new Pathdashpatheffect (Makepathdash (), Phase,

PathDashPathEffect.Style.MORPH);

E[6] = new Composepatheffect (e[2], e[1]);

E[7] = new Sumpatheffect (e[2], e[1]);

E[8] = new Composepatheffect (e[5], e[1]);

E[9] = new Sumpatheffect (e[5], e[1]);

}

Make a shape, pathdashpatheffect the unit shape displayed

private static Path Makepathdash () {

Path p = new Path ();

P.moveto (4, 0);

P.lineto (0,-4);

P.lineto (8,-4);

P.lineto (12, 0);

P.lineto (8, 4);

P.lineto (0, 4);

return p;

}

6.4.6-point class Android.graphics.Point and Android.graphics.PointF

Having seen the lines drawn by canvas, let's take a look at the basis of the lines that make up the line (Point Class).

The point class has two properties, namely, X-coordinate and y-coordinate.

There are three constructor functions.

Point ()//Construct a dot

Point (int x,int y)//pass in X and Y coordinates to construct a dot

Point P//pass-through to a points object constructs a dot

The main method is shown in table 6-6.

Method

return value

Description

Set (x, y)

void

Re-set X, y coordinates

offset (int dx,int dy)

void

Give the coordinates a compensating value so that the positive or negative values can be

Negate ()

void

Negative coordinate values

Table 6-6 methods common to point classes

The point class is similar to Android.graphics.PointF, where the difference is that the type of the coordinate value is the int type, and the coordinate value of the latter is the float type.

In addition, the PointF class adds several methods, such as:

Public final float length ();//Returns (0,0) the distance to the point.

public static float Length (float x,float y);//Returns the distance (0,0) point to (x, y) point.

Experience Sharing:

Speaking of coordinate points, then we have to say the cell phone screen coordinate system, the phone's coordinate system and the general physical coordinate system, the cell phone screen coordinate system origin (0,0) in the upper left corner of the screen, along the left edge and along the edge, X, y values are increased sequentially. As shown in 6-3.

Figure 6-3 Cell Phone coordinate point

6.4.7 shape Classes Android.graphics.Rect and Android.graphics.RectF

Rectangles, one of several commonly used shapes on a drawing. RECTF This class contains four single-precision floating-point coordinates for a rectangle. The rectangle represents a rectangle by the coordinates of the top, bottom, left, and right 4 edges. These coordinate value properties can be accessed directly, and the width () and height () methods can be used to obtain the width and height of the rectangle.

RECTF altogether has four construction methods.

RECTF ()//construct a non-parametric rectangle

RECTF (float left,float top,float right,float bottom)//construct a rectangle with 4 parameters specified

RECTF (RECTF R)//construct a RECTF object (copy a rect F) according to the specified RECTF object

RECTF (Rect R)//constructs a RECTF object based on the given Rect object

RECTF provides a number of methods, as shown in table 6-7 below, which describes several methods.

Method

return value

Description

Contain (RECTF R)

Boolean

Determines whether a point or rectangle is within this rectangle, and returns True if within the rectangle or equivalent to the rectangle.

Offset (float dx, float dy)

void

Pan Dx,dy Distance

Offsetto (float newleft, float newtop)

void

Pan to the new location

Inset (float dx, float dy)

void

Reduce 2*dx,2*dy

Table 6-7 Common methods of RECTF class

Experience Sharing:

The Android.graphics.Rect class, which resembles Android.graphics.RectF very similar, differs in that the coordinates of a RECT class are represented by an integral type, while the coordinates of RECTF are represented by a single-precision floating-point type.

Get the zoom ratio of x in matrix:

public void GetValues (float[] values);

The array values are an array of size>9, and values [matrix.mscale_x] are scaled proportionally. Other parameters are also included: Matrix.

public static final int mscale_x = 0;

public static final int mskew_x = 1;

public static final int mtrans_x = 2;

public static final int mskew_y = 3;

public static final int mscale_y = 4;

public static final int mtrans_y = 5;

public static final int mpersp_0 = 6;

public static final int mpersp_1 = 7;

public static final int mpersp_2 = 8;

Sixth chapter a piece of white paper good painting-canvas canvas (4)

Related Article

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.