灵活使用 Path ,能够画出复杂图形,就像美术生在画板上画复杂图形同样。程序员也能够用代码实现。android
这个是个温度计,它是静态的,温度值是动态变化的,因此要自定义个view.动态显示值,温度太高、太低时有警示功能。git
https://github.com/f9q/tempView程序员
将画笔移动到x,ygithub
链接当前点到目标点dp(x,y),画一条线。算法
从当前点开始画弧,startAngle是順时针起始角度,sweepAngle是扫过角度(按360求模)。若是oval的起点不是当前path的最后一个点,则链接最后一个点到oval的起点。api
假设最后个点是op(10,20),若是没有用MoveTo指定,默认点是(0,0),画一条通过 op、控制点cp(x1,y1)、终点ep(x2,y2)的贝塞尔曲线. 数组
与quadTo类似,只不过多个控制点cp2(x2,y2)app
假设前个点是op(10,20),将画笔移动到目标点dp(dx,dy), 其中dx是相对10的偏移量,dy是相对20的偏移量。dom
假设前个点是op(10,20), 链接当前点到目标点dp(dx,dy),画一条线。其中dx是相对10的偏移量,dy是相对20的偏移量ide
假设前个点是op(10,20),默认点是(0,0),画一条通过op、控制点cp(dx1,dy1)、终点ep(dx2,dy2)的贝塞尔曲线。dx1,dx2是相对前个点10的x偏移量,可为负。
dy1,dy2是相对前个点20的y偏移量,可为负。
与rQuadTo类似。也是多个了个控制点cp2(x2,y2)
在路径上添加一个矩形、第2个参数是添加矩形的绘制方向,
与上一个类似,添加的是圆角矩形。dir是纵向方向,如上。
添加一个椭圆,dir是纵向方向,如上。
添加一个圆圈,radius半径,dir是纵向方向。如上。
添加一个弧线,startAngle是順时针起始角度,sweepAngle是扫过角度(按360求模)。
在当前路径上添加一个新的路径,dx,dy是相对最后一个点的x,y偏移量。
把当前路径按matrix变形,若是dst不空,复制一分到dst中。
清空path对象数据和轨迹。保留FillType。
清空所绘制的轨迹,但保留对象内部数据。方便下次快速复用。 不保留FillType。
这个比较复杂,见5.path.setFillType(FillType ft)
void close()
闭合路径,若是执行闭合操做时当前点不和第1个点重合,则会画出一条线。
当前路径总体(全部图形)平移。
能够给path添加各类特效,如虚线,离散等等。
效果:将路径的转角变成圆角。
代码:
1 paint.setPathEffect(new CornerPathEffect(30));
参数:
圆角的半径。
效果:离散路径。
代码:
1 paint.setPathEffect(new DiscretePathEffect(3.0f, 10.0f));
参数:
/** * Chop the path into lines of segmentLength, randomly deviating from the * original path by deviation. */
效果:虚线效果。
代码:
1 float intervals[] = {1,5,10,15}; 2 paint.setStrokeWidth(5); 3 paint.setStyle(Paint.Style.STROKE); 4 paint.setPathEffect(new DashPathEffect(intervals, 3));
DashPathEffect参数的含义:
/** * The intervals array must contain an even number of entries (>=2), with * the even indices specifying the "on" intervals, and the odd indices * specifying the "off" intervals. phase is an offset into the intervals * array (mod the sum of all of the intervals). The intervals array * controls the length of the dashes. The paint's strokeWidth controls the * thickness of the dashes. * Note: this patheffect only affects drawing with the paint's style is set * to STROKE or FILL_AND_STROKE. It is ignored if the drawing is done with * style == FILL. * @param intervals array of ON and OFF distances * @param phase offset into the intervals array */
控制虚线的长度与间隙,数组长度不能小于2个,偶数位表示实线线段的长度(上面数组中的一、10),奇数位表示间隙的长度(上面数组中的五、15)。
控制虚线第1段是线段仍是间隙,按第1个参数的长度求模。
注意:这个特效只有在 paint的风格是 STROKE 和 FILL_AND_STROKE 时有效。
效果:指定虚线上的每一个线段的图形为另外一个路径。
代码:
1 Path rectDash = new Path(); 2 rectDash.addRect(0, 0, 10,10, Path.Direction.CW); 3 paint.setPathEffect(new PathDashPathEffect(rectDash,20,1, PathDashPathEffect.Style.MORPH));
参数:
MORPH、ROTATE、TRANSLATE 3种。
效果:组合效果
代码:
1 float intervals[] = {1,5,10,15}; 2 DiscretePathEffect discrete = new DiscretePathEffect(3.0f, 10.0f); 3 DashPathEffect dash = new DashPathEffect(intervals, 3) ; 4 ComposePathEffect compose = new ComposePathEffect(discrete,dash); 5 paint.setPathEffect(compose);
参数:
/** * Construct a PathEffect whose effect is to apply first the inner effect * and the the outer pathEffect (e.g. outer(inner(path))). */ public ComposePathEffect(PathEffect outerpe, PathEffect innerpe) { native_instance = nativeCreate(outerpe.native_instance, innerpe.native_instance); }
第1个路径和第2个路径。先画出innerpe的效果,接着在innerpe的基础上增长outerpe效果!
效果:叠加效果
代码:
1 float intervals[] = {1,5,10,15}; 2 DiscretePathEffect discrete = new DiscretePathEffect(3.0f, 10.0f); 3 DashPathEffect dash = new DashPathEffect(intervals, 3) ; 4 SumPathEffect sum = new SumPathEffect(discrete,dash); 5 paint.setPathEffect(sum);
参数:
/** * Construct a PathEffect whose effect is to apply two effects, in sequence. * (e.g. first(path) + second(path)) */ public SumPathEffect(PathEffect first, PathEffect second) { native_instance = nativeCreate(first.native_instance, second.native_instance); }
先画出first的效果,接着绘制second效果!将两个效果简单的重叠在一块儿显示出来!
它指定计算路径内部区域的算法。如何判断点在图形内外,通常有两种:奇偶算法和非零环绕数算法。
1 /** 2 * Set the path's fill type. This defines how "inside" is computed. 3 * 4 * @param ft The new fill type for this path 5 */ 6 public void setFillType(FillType ft) { 7 nSetFillType(mNativePath, ft.nativeInt); 8 } 9 /** 10 * Enum for the ways a path may be filled. 11 */ 12 public enum FillType { 13 // these must match the values in SkPath.h 14 /** 15 * Specifies that "inside" is computed by a non-zero sum of signed 16 * edge crossings. 17 */ 18 WINDING (0), 19 /** 20 * Specifies that "inside" is computed by an odd number of edge 21 * crossings. 22 */ 23 EVEN_ODD (1), 24 /** 25 * Same as {@link #WINDING}, but draws outside of the path, rather than inside. 26 */ 27 INVERSE_WINDING (2), 28 /** 29 * Same as {@link #EVEN_ODD}, but draws outside of the path, rather than inside. 30 */ 31 INVERSE_EVEN_ODD(3); 32 33 FillType(int ni) { 34 nativeInt = ni; 35 } 36 37 final int nativeInt; 38 }
EVEN_ODD | 奇偶算法 |
INVERSE_EVEN_ODD | 反奇偶算法 |
WINDING | 非零环绕数算法 |
INVERSE_WINDING | 反非零环绕数算法 |
算法:
从p点向任意方向做一条射线, 若该射线与图形相交的边数为奇数,则p是图形内部点,不然是外部点。
示例:
红色的点在多边形内部。从它出发做任意射线(图中画出了两种可能),它与多边形的边一定相交奇数次(图中两条射线分别相交1次和3次)。
蓝色的点在多边形外部。从它出发做任意射线(图中画出了两种可能),它与多边形的边一定相交偶数次(图中两条射线分别相交0次和2次)。
示例:
源码:与其它path进行运算。它还有一个重载版本。
1 /** 2 * Set this path to the result of applying the Op to the two specified paths. 3 * The resulting path will be constructed from non-overlapping contours. 4 * The curve order is reduced where possible so that cubics may be turned 5 * into quadratics, and quadratics maybe turned into lines. 6 * 7 * @param path1 The first operand (for difference, the minuend) 8 * @param path2 The second operand (for difference, the subtrahend) 9 * 10 * @return True if operation succeeded, false otherwise and this path remains unmodified. 11 * 12 * @see Op 13 * @see #op(Path, android.graphics.Path.Op) 14 */ 15 public boolean op(Path path1, Path path2, Op op) { 16 if (nOp(path1.mNativePath, path2.mNativePath, op.ordinal(), this.mNativePath)) { 17 isSimplePath = false; 18 rects = null; 19 return true; 20 } 21 return false; 22 } 23 /** 24 * The logical operations that can be performed when combining two paths. 25 * 26 * @see #op(Path, android.graphics.Path.Op) 27 * @see #op(Path, Path, android.graphics.Path.Op) 28 */ 29 public enum Op { 30 /** 31 * Subtract the second path from the first path. 32 */ 33 DIFFERENCE, 34 /** 35 * Intersect the two paths. 36 */ 37 INTERSECT, 38 /** 39 * Union (inclusive-or) the two paths. 40 */ 41 UNION, 42 /** 43 * Exclusive-or the two paths. 44 */ 45 XOR, 46 /** 47 * Subtract the first path from the second path. 48 */ 49 REVERSE_DIFFERENCE 50 }
参数:前两个参数是参与运算的path,第3个参数是路径运算符。
运算符 | 类比集合运算 | 说明 | 示意图 |
---|---|---|---|
XOR | 异或 | 求非交集。 | ![]() |
INTERSECT | 交集 | Path1与Path2相交的部分 | ![]() |
UNION | 并集 | 包含所有Path1和Path2 | ![]() |
DIFFERENCE | 差集 | Path1中减去Path2后剩下的部分 | ![]() |
REVERSE_DIFFERENCE | 差集 | Path2中减去Path1后剩下的部分 | ![]() |