Path的一些参数

2020-12-01  本文已影响0人  有点健忘

1. FillType

path的填充方式,有4种,举例说明啥效果

    /**
     * Enum for the ways a path may be filled.
     */
    public enum FillType {
        // these must match the values in SkPath.h
        /**
         * Specifies that "inside" is computed by a non-zero sum of signed
         * edge crossings.
         */
        WINDING         (0),
        /**
         * Specifies that "inside" is computed by an odd number of edge
         * crossings.
         */
        EVEN_ODD        (1),
        /**
         * Same as {@link #WINDING}, but draws outside of the path, rather than inside.
         */
        INVERSE_WINDING (2),
        /**
         * Same as {@link #EVEN_ODD}, but draws outside of the path, rather than inside.
         */
        INVERSE_EVEN_ODD(3);

        FillType(int ni) {
            nativeInt = ni;
        }

        final int nativeInt;
    }

1.1 WINDING

默认值就是这个,测试代码如下,数学里的并集,属于A或者属于B的部分

        path2.apply {
            reset()
            addCircle(100f,100f,50f,Path.Direction.CW)
        }

        path3.apply {
            reset()
            addCircle(200f,200f,50f,Path.Direction.CW)
        }
        path.apply {
            reset()
            path.addPath(path2)
            path.fillType=Path.FillType.WINDING
            path.addPath(path3)
        }
image.png
image.png

1.2 INVERSE_WINDING

上边的效果取反,
测试代码

        path2.apply {
            reset()
            addCircle(100f,100f,50f,Path.Direction.CW)
        }

        path3.apply {
            reset()
            addCircle(200f,200f,50f,Path.Direction.CW)
        }

        path.apply {
            reset()
            path.addPath(path2)
            path.fillType=Path.FillType.INVERSE_WINDING
            path.addPath(path3)
        }

canvas.drawPath(path,p)
image.png
image.png

1.3 EVEN_ODD

把交集扣掉,也就是把两者共有的部分扣掉

        path3.apply {
            reset()
            addCircle(200f,200f,50f,Path.Direction.CW)
        }

        path4.apply {
            reset()
            addCircle(280f,200f,60f,Path.Direction.CW)
        }

        path.apply {
            reset()
            path.addPath(path3)
            path.fillType=Path.FillType.EVEN_ODD
            path.addPath(path4)
        }
image.png

1.4 INVERSE_EVEN_ODD

上边的效果取反


image.png

Op介绍

先看几张图,我们的path如下红线


image.png

实际需求中,我们需要下图紫色的区域,咋办


image.png

先加上下图的梯形再说


image.png

效果如下


image.png
这时候就知道咋办了,把左右两边2个弧形踢出去就刚刚好,而path也刚好有这个方法
image.png

如下,path3,path4就是那2个圆弧

path.op(path3,Path.Op.DIFFERENCE)
            path.op(path4,Path.Op.DIFFERENCE)

简单看下Op的属性

    public enum Op {
        /**
         * Subtract the second path from the first path.简单理解为把括号里的path从当前path里抠掉
         */
        DIFFERENCE,
        /**
         * Intersect the two paths. 两条路径相交的部分
         */
        INTERSECT,
        /**
         * Union (inclusive-or) the two paths.
         */
        UNION,
        /**
         * Exclusive-or the two paths.
         */
        XOR,
        /**
         * Subtract the first path from the second path.
         */
        REVERSE_DIFFERENCE
    }

下面简单写下代码和效果图

DIFFERENCE

先红圈,再蓝圈,完事从红圈里扣除篮圈,也就是黄色的月牙部分了


image.png
        val path1=Path().apply {
            addCircle(0f,0f,100f,Path.Direction.CW)
        }
        val path2=Path().apply {
            addCircle(50f,0f,100f,Path.Direction.CW)
        }

        p.color=Color.RED
        canvas.drawPath(path1,p)
        p.color=Color.BLUE
        canvas.drawPath(path2,p)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            p.color=Color.YELLOW
            path1.op(path2,Path.Op.DIFFERENCE)
            canvas.drawPath(path1,p)
        }
INTERSECT

代码同上,就是Op改下而已,效果图如下,交集,中间的部分


image.png
UNION

合集,黄色部分


image.png
XOR

和交集刚好相反,去同存异,两条path不一样的地方


image.png
REVERSE_DIFFERENCE

和DIFFERENCE 差不错,这个是把当前路径从参数path里扣除,而DIFFERENCE是从当前路径把参数里的path扣除
换句话说,如下两种代码效果是一样的

path1.op(path2,Path.Op.DIFFERENCE)
 canvas.drawPath(path1,p)
path2.op(path1,Path.Op.REVERSE_DIFFERENCE)
            canvas.drawPath(path2,p)
image.png

实际使用

那个做翻页效果的时候会用到,我们需要扣除圆弧的部分。

上一篇 下一篇

猜你喜欢

热点阅读