标签:
用到的工具类:
DashPathEffect
android中对该类的说明如下:
翻译一下就是:
第一个参数intervals数组必须是偶数个元素(个数>=2),偶数下标代表实线,奇数下标代表空白长度
第二个参数phase:向左偏移量(偏移量=phase mod (intervals数组各项之和), 即取余运算)
注意:只有当Paint的style设置为STROKE或者FILL_AND_STROKE时才能绘制出虚线效果,如果Paint的style设置为FILL时是不会起作用的。
/** * 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 */ public DashPathEffect(float intervals[], float phase) { if (intervals.length < 2) { throw new ArrayIndexOutOfBoundsException(); } native_instance = nativeCreate(intervals, phase); }
初始化paint, Path
private Paint mDottedLinePaint; private Path mDottedPath; // 虚线paint mDottedLinePaint = new Paint(); mDottedLinePaint.setColor(getResources().getColor(R.color.horizontal_bar_chart_dot_line_color)); mDottedLinePaint.setStyle(Style.STROKE); mDottedLinePaint.setStrokeWidth(2); PathEffect effects = new DashPathEffect(new float[]{ 10, 5}, 1);//意思是所画虚线规则是先画10个长度的实线,留下5个长度的空白 mDottedLinePaint.setPathEffect(effects); mDottedPath = new Path();
画虚线
canvas.drawPath(mDottedPath, mDottedLinePaint);
这里需要注意,一定要用canvas的drawPath方法,如果用drawLine的话是画不出效果的,原来在网上找的代码就是用的drawLine,结果半天出不来效果
标签:
原文地址:http://www.cnblogs.com/crack-me/p/4725934.html