Android Canvas 图形绘制 -> Xfermode

打算 找几篇吊的 文章,跑一跑就通了

1.入门1

Android Canvas 图形绘制 -> Xfermode_第1张图片

    Paint mPaint = new Paint();
        mPaint.setColor(Color.BLUE);

        canvas.drawCircle(300,300 , 300, mPaint);

        mPaint.setColor(Color.RED);

        RectF rectF = new RectF(300, 300, 700, 700);
        canvas.drawRect(rectF,mPaint);
  

这里边有重合的


Android Canvas 图形绘制 -> Xfermode_第2张图片

setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        canvas.drawARGB(255, 139, 197, 186);

        Paint mPaint = new Paint();
        mPaint.setColor(Color.BLUE);

        canvas.drawCircle(300,300 , 300, mPaint);

        //使用CLEAR作为PorterDuffXfermode绘制蓝色的矩形
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

        mPaint.setColor(Color.RED);

        RectF rectF = new RectF(300, 300, 700, 700);
        canvas.drawRect(rectF,mPaint);

        //最后将画笔去除Xfermode
        mPaint.setXfermode(null);

第一行那个必须写,貌似硬件加速和这个冲突


Android Canvas 图形绘制 -> Xfermode_第3张图片

    setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

        Paint mPaint = new Paint();
        mPaint.setColor(Color.BLUE);

        canvas.drawCircle(300,300 , 300, mPaint);

        //使用CLEAR作为PorterDuffXfermode绘制蓝色的矩形
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

        mPaint.setColor(Color.RED);

        RectF rectF = new RectF(300, 300, 700, 700);
        canvas.drawRect(rectF,mPaint);

        //最后将画笔去除Xfermode
        mPaint.setXfermode(null);

        canvas.restoreToCount(layerId);

Android Canvas 图形绘制 -> Xfermode_第4张图片


        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

        Paint mPaint = new Paint();
        mPaint.setColor(Color.BLUE);

        canvas.drawCircle(300,300 , 300, mPaint);

        //使用CLEAR作为PorterDuffXfermode绘制蓝色的矩形
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));

        mPaint.setColor(Color.RED);

        RectF rectF = new RectF(300, 300, 700, 700);
        canvas.drawRect(rectF,mPaint);

        //最后将画笔去除Xfermode
        mPaint.setXfermode(null);

        canvas.restoreToCount(layerId);

 

Android Canvas 图形绘制 -> Xfermode_第5张图片

 

这图我偷的


先画三个波浪

Android Canvas 图形绘制 -> Xfermode_第6张图片

public class MyoWaveView extends View {

    private Paint mPaint;
    private int bochang=300;//波长 一朵浪花的 宽度
    private int bogao=200; //波高  一朵浪花的 高度
    private Path path;


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

    public MyoWaveView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyoWaveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mPaint = new Paint();

        mPaint.setColor(Color.BLUE);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int height = getHeight();
        int y = height / 2;

        path = new Path();
        path.moveTo(0, y);
        path.quadTo(bochang/2,bogao,bochang,y);
        path.quadTo(bochang+bochang/2,bogao,bochang*2,y);
        path.quadTo(bochang+bochang+bochang/2,bogao,bochang*3,y);


        path.lineTo(bochang*3,height);
        path.lineTo(0,height);
        path.close();

        canvas.drawPath(path, mPaint);

    }
}

紧接着画一个大圆

 //画一个大圆
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        mPaint.setColor(Color.YELLOW);
        canvas.drawCircle(getWidth()/2,getHeight()/2,getWidth()/2,mPaint);

Android Canvas 图形绘制 -> Xfermode_第7张图片

然后Xfermode  只要重合的,额,失败了,留个纪念


 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Android Canvas 图形绘制 -> Xfermode)