码迷,mamicode.com
首页 > 移动开发 > 详细

android 画虚线

时间:2015-08-13 00:54:54      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

 

 

用到的工具类:

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,结果半天出不来效果

android 画虚线

标签:

原文地址:http://www.cnblogs.com/crack-me/p/4725934.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!