Android Canvas 裁剪 Region.Op.REPLACE

实现类似效果:
在这里插入图片描述
主要功能代码:

package myapplication.lum.com.bird;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Region;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {
    private  Paint mPaint;
    public MyView(Context context) {
        super(context);
        init();
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

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

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.translate(10, 10);
        canvas.save();
        canvas.translate(560, 0);

        mPaint.setColor(Color.RED);
        canvas.drawCircle(200,200,100,mPaint);
        Path pathOne = new Path();
        pathOne.addCircle(200,200,100,Path.Direction.CW);
        canvas.clipPath(pathOne);

        mPaint.setColor(Color.BLUE);
        canvas.drawCircle(300,200,100,mPaint);
        Path pathtwo = new Path();
        pathtwo.addCircle(300,200,100,Path.Direction.CW);
        canvas.clipPath(pathtwo,Region.Op.REPLACE);

        drawScene(canvas);
        canvas.restore();

    }

    private void drawScene(Canvas canvas){
        canvas.drawRect(0, 0, getRight(), getBottom(), mPaint);
    }


    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10);
        mPaint.setColor(Color.YELLOW);
    }
}

文章参考:
Canvas API详解(Part 2)剪切方法合集

你可能感兴趣的:(Android,自定义组件,View)