Path class introduces common API

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u011118524/article/details/78348138
/**
 * The Path class encapsulates compound (multiple contour) geometric paths consisting of straight line segments, quadratic curves, and cubic curves.
 * It can be drawn with canvas.drawPath(path, paint), either filled or stroked
 * (based on the paint's Style), or it can be used for clipping or to draw
 * text on a path.
 */

Translation: the Path encapsulate geometric path from the straight lines and curves (quadratic, cubic Bezier curve) configuration. You can use the Canvas drawPath to put the path drawn (also supports different rendering modes of Paint), it can also be used to cut the canvas to draw text and the path. We sometimes use Path to describe a contour image, it is also called contour lines (contour lines is only one method of use of the Path, the two are not equivalent).

As can be seen from comments, Path class encapsulates some complex geometric path, including linear, quadratic curve, a cubic curve, and what can be done graph. We look at the source code, which has an internal class Direction, in a series we call the add method to add graphics to the Path Path inside and they will need to provide a direction, such as we call addRect way to add a rectangle Rect ( 100,100,200,200) when assuming four vertices of the rectangle ABCD, the coordinates are a (100,100), B (200,100), C (200,200), D (200,100), to add in Paht in time, Path specified based direction, configured to parse each point of the straight line, if the specified direction is clockwise, then the order of four rectangular sides and drawn followed by adding AB, BC, CD, DA; if counterclockwise, then followed AD, DC, CB, BA, the following will be verified by our ideas code examples.

Direction source as follows:

/**
* Specifies how closed shapes (e.g. rects, ovals) are oriented when they
* are added to a path.
*/
public enum Direction {
/** 顺时针方向*/
CW  (0),    // must match enum in SkPath.h
/** 逆时针方向 */
CCW (1);    // must match enum in SkPath.h

Direction(int ni) {
    nativeInt = ni;
}
final int nativeInt;
}

Test code is as follows:

mPath.addRect(200, 100, 500, 300, Path.Direction.CW);
canvas.drawPath(mPath, mPaint);
mPath.reset();
mPath.addRect(200, 400, 500, 600, Path.Direction.CCW);
canvas.drawPath(mPath, mPaint);

The effect is as follows:
Write pictures described here
From the renderings implementation, we found that, when drawing a regular pattern of clockwise and counterclockwise basically no difference, in order to see the difference, we modify the above code as follows:

setLayerType(LAYER_TYPE_SOFTWARE, null);//需要关闭硬件加速功能,否则画出的形状是不闭合的,

mPath.addRect(100, 100, 300, 300, Path.Direction.CW);
mPath.setLastPoint(50, 200);
mPath.close();
canvas.drawPath(mPath, mPaint);

mPath.reset();
mPath.addRect(100, 400, 300, 600, Path.Direction.CCW);
mPath.setLastPoint(50, 500);
mPath.close();//使图形闭合
canvas.drawPath(mPath, mPaint);

Achieve results is as follows:
Write pictures described here

When we specify a draw last time point, clockwise and counterclockwise can see the difference.

When we draw the text on the specified path, the direction of the text will be consistent with the direction we add the path to be used.
Such as:

mPath.addCircle(400, 400, 300, Path.Direction.CW);
canvas.drawPath(mPath, mPaint);
canvas.drawTextOnPath("顺时针方向绘制文字", mPath, 25, 25, mPaint);

mPath.reset();
mPath.addCircle(400, 1100, 300, Path.Direction.CCW);
canvas.drawPath(mPath, mPaint);
canvas.drawTextOnPath("逆时针方向绘制文字", mPath, 25, 25, mPaint);

The effect is as follows:
Write pictures described here

The above describes the basic usage Path.Direction about the class, the next will introduce Path common Api.

Common Path Api Description:

Method name The method described
moveTo Starting position to the next operation
setLastPoint Reset the last position of the current point in the path, if called before drawing the same, and the effect moveTo
lineTo Adding a straight line between the point to the current point in Path
close Connecting a first point to the last point, so as to form a closed area
addRect, addRoundRect, addOval, addCircle, addPath, addArc, arcTo Was added (rectangles, rounded rectangles, ellipses, circles, paths, arc) to the current Path (note the difference in addArc and arcTo)
isEmpty Determine whether the Path is empty
isRect Determining whether the path is a rectangle
set Replace all the contents of the current path with a new path
offset Before the operation of the current path offset (after the operation will not affect)
quadTo, cubicTo Respectively second and third order Bezier curve adding method
rmoveto, rlineto, rQuadTo, rCubicTo The method is based on without r coordinate origin (offset), rXxx method is based on the current point (i.e., the current point as the coordinate origin) coordinate system (offset)
setFillType, getFillType, isInverseFillType, toggleInverseFillType Set, Get, and a switching determination fill mode
incReserve Tip Path how many points waiting to join (this method seems to make Path optimized storage structure)
on Path Boolean operations on two (i.e., on the intersection, and the like set operations)
computeBounds Calculation of boundary Path
reset, rewind Clear the contents of the Path, reset does not retain the internal data structures, but retain FillType; rewind retains internal data structures, but does not retain FillType
transform 矩阵变换

Path常用Api实例详解

在UI绘制的时候,硬件加速在某些情况下会引起一些绘制问题,如上面的绘制,在没有关闭硬件加速的时候,绘制的图形是不闭合的,所以为了避免不必要的麻烦,在绘制之前关闭硬件加速,关闭的方式有如下几种方式:

  • setLayerType(LAYER_TYPE_SOFTWARE, null);在自定义控件初始化的时候设置
  • 在AndroidMenifest文件中application节点下添上 android:hardwareAccelerated=”false”

Path添加圆弧的方法:addArc,arcTo

// addArc
public void addArc (RectF oval, float startAngle, float sweepAngle);
// arcTo
public void arcTo (RectF oval, float startAngle, float sweepAngle);
/**
 * Append the specified arc to the path as a new contour. If the start of
 * the path is different from the path's current last point, then an
 * automatic lineTo() is added to connect the current contour to the
 * start of the arc. However, if the path is empty, then we call moveTo()
 * with the first point of the arc.
 *
 * @param startAngle  开始角度
 * @param sweepAngle  偏移角度,即绘制多少角度的圆弧
 * @param forceMoveTo true把
 */
public void arcTo (RectF oval, float startAngle, float sweepAngle, boolean forceMoveTo)

注释翻译:添加一个指定的圆弧到Path中,如果被添加的圆弧的起点与当前Path路径的最后一个点不相同,那么将会自动的调用lineTo方法,把前路径最后一个点与圆弧的起点用直线连接起来。如果当前Path为空,则调用moveTo方法,把圆弧的第一个点设置为起点。

通过注释我们可以了解到,如果forceMoveTo参数为true,那么会调用moveTo方法把被圆弧的起点设置为当前路径的最后一个点,这样当前Path路径的最后一个点的坐标与被添加圆弧的起点坐标是一样的,这样就不会调用lineTo方法去连线了,如果forceMoveTo参数为false,那么当前Path路径的最后一个点没有改变,该是啥就是啥,如果与被添加的圆弧起点不一致,则会调用lineTo方法添加一条连线,把当前Path的最后一个点与圆弧的起点连接起来。默认为false。

实例代码:

Path path = new Path();
path.lineTo(100, -100);
path.arcTo(new RectF(-300, -300, -100, -100), 0, 270);//
path.arcTo(new RectF(-50, -50, 100, 100), 0, 270, true);
canvas.drawPath(path, paint);

效果图如下:
Write pictures described here

说明:

  • path.lineTo(100, -100);方法添加了一条直线1,执行完后,Path的最后一个点为A,
  • path.arcTo (new RectF (-300, -300, -100, -100), 0, 270); a circular arc is added to the Path 2, arcTo method forceMoveTo default argument is false, so when performing the method arcTo I found this is the last point of Path a, but starting coordinate a and B were added 2 arc inconsistent, it will call the lineTo method, the connecting points a and B, so there is a line 4. The order of execution: 4 to draw a straight line, arc 2 in Videos. After thus executing the code, the last point is C. Path
  • path.arcTo (new RectF (-50, -50, 100, 100), 0, 270, true); a circular arc added to Path 3, this time forceMoveTo parameter is true, the first call moveTo method (parameter the coordinates D), then the current becomes D last point of the Path, and the coordinates of the starting point of the arc 3 is the same, so we did not call the lineTo method to draw a straight line. After executing the code, the last point of the current Path of E.

Summary: addArc is simply to add a circular arc, when the forceMoveTo arcTo = true and methods, and AddArc equivalent method.

It recommended a well-written blog: Andrews View from basic operational definition of advanced -Path

Guess you like

Origin blog.csdn.net/u011118524/article/details/78348138