android Path.setFillType(Path.FillType ft) 设置填充方式

参考链接

http://hencoder.com/ui-1-1/

参考图

这里写图片描述

方法中填入不同的 FillType 值,就会有不同的填充效果。FillType 的取值有四个:

EVEN_ODD
WINDING (默认值)
INVERSE_EVEN_ODD
INVERSE_WINDING
其中后面的两个带有 INVERSE_ 前缀的,只是前两个的反色版本,所以只要把前两个,即 EVEN_ODD 和 WINDING,搞明白就可以了。

代码

public class PathFillTypeView  extends View{
    private Paint mPaint;
    //模式
    private Path.FillType[] mModes = {
            Path.FillType.WINDING,
            Path.FillType.EVEN_ODD,
            Path.FillType.INVERSE_WINDING,
            Path.FillType.INVERSE_EVEN_ODD,
    };
    //
    private int mTitleIndex = 0;

    public PathFillTypeView(Context context) {
        this(context,null);
    }

    public PathFillTypeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PathFillTypeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    /** 初始化画笔 */
    private void initPaint(){
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Path path = new Path();
        path.setFillType(mModes[mTitleIndex]);
        path.addCircle(100,100,100, Path.Direction.CCW);
        path.addCircle(150,150,100, Path.Direction.CCW);
        canvas.drawPath(path,mPaint);
    }

    //切换模式 并返回现在的模式名
    public String change() {
        mTitleIndex++;
        if (mTitleIndex >= mModes.length){
            mTitleIndex = 0;
        }
        invalidate();
        return mModes[mTitleIndex].name();
    }
}

各模式演示

android Path.setFillType(Path.FillType ft) 设置填充方式_第1张图片

你可能感兴趣的:(Android,View篇)