Custom Controls (2): usage of the Path class and detailed description of the path
Path: Path
Drawing Path: void drawPath (path Path, Paint paint)
Paths that can be drawn
1. Straight Line path
1. Basic Methods
Void moveTo (float startX, float startY) is the starting point of a straight line, that is, the drawing point of a straight line path is set at the position of (startX, startY); void lineTo (float endX, float endY) the end point of a straight line is the start point of the next line path. If void close () draws several straight lines consecutively but does not form a closed loop, call Close () connects the first and end points of the path to form a closed loop;
1. No closed loop is formed
------------>
② Forming a closed loop. We can see that a straight line is closed between the first and last coordinates.
------------>
Ii. rectangular path
1. Basic Methods
void addRect (float left, float top, float right, float bottom, Path.Direction dir) void addRect (RectF rect, Path.Direction dir)
RectF rect = new RectF(float left, float top, float right, float bottom);
The two methods are similar.
Here is a Path. Direction dir parameter.
There are two system values:
(1) Path. Direction. CCW: it is the abbreviation of counter-clockwise, which refers to creating a rectangle Path in the counterclockwise Direction;
(2) Path. Direction. CW: the abbreviation of clockwise, which refers to creating a clockwise rectangular Path;
Note: differences between RectF and Rect
1. Both represent rectangles.
2. The precision is different. The Rect is int, And the RectF is float.
3. The RectF class is used for the rectangular path. Do not use the Rect class.
------------>
3. circular path
1. Basic Methods
void addCircle (float x, float y, float radius, Path.Direction dir)
In one way, the parameter is clear: x center x axis coordinate, y center y axis coordinate, radius circle radius, dir draw the direction of the circular path
------------>
Iv. Elliptical Path
1. Basic Methods
void addOval (RectF oval, Path.Direction dir)
RectF rect = new RectF(float left, float top, float right, float bottom);
We can find that the RectF class is similar to that used to draw a rectangular path.
Note the meaning of the four parameters
------------>
V. Arc path
1. Basic Methods
void addArc (RectF oval, float startAngle, float sweepAngle)
Meanings of the two parameters:
① Float startAngle: the starting angle. The positive direction of the X axis is 0 degrees.
② Float sweepAngel: continuous level;
Draw an arc starting from the X-axis square (0 °) lasting 270 °
------------>
Vi. rectangular path of rounded corners
1. Basic Methods
void addRoundRect (RectF rect, float[] radii, Path.Direction dir) void addRoundRect (RectF rect, float rx, float ry, Path.Direction dir)
There are two methods:
① Void addRoundRect (RectF rect, float [] radii, Path. Direction dir)
One float [] radii parameter is added. This parameter controls the degree of the circle of the four corners of the circular rectangle. Each angle is controlled by one X axis radius and one Y axis radius.
Therefore, this array parameter must have eight values. This method can control the degree of the circle of each corner of the generated rounded rectangle.
Each two values clockwise from the upper left corner controls an angle.
In the following example, we can see that the degrees of the instantaneous start angle in the upper left corner are 5px, 10px, 15px, and 20px respectively.
------------>
② Void addRoundRect (RectF rect, float rx, float ry, Path. Direction dir)
This method is simpler. You only need to control the radius of x and Y axes once, so that the four corners can be set once.
For example, the degrees of each angle are consistent.
------------>
Bytes ------------------------------------------------------------------------------------------------------------------------------
The above are various ways to draw the path. Next we will talk about the parameters that are basically required for path painting.
1. Path. Direction. CCW: it is the abbreviation of counter-clockwise. It refers to creating a rectangle Path in the counterclockwise Direction;
2. Path. Direction. CW: the abbreviation of clockwise, which refers to creating a clockwise rectangular Path;
It is easy to see that this parameter is actually used to control whether to draw a drawing clockwise or counterclockwise.
Now we can easily see the order of drawing by adding text to the painting.
----------->
Reference Code