PorterDuffXfermode之PorterDuff.Mode.XOR入门学习

Posted loaderman

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PorterDuffXfermode之PorterDuff.Mode.XOR入门学习相关的知识,希望对你有一定的参考价值。

package com.loaderman.customviewdemo.view;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View;


public class PorterDuffXfermodeView extends View {
    private int width = 100;
    private int height = 100;
    private Bitmap dstBmp;
    private Bitmap srcBmp;
    private Paint mPaint;

    public PorterDuffXfermodeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        dstBmp = makeDst(width, height);
        srcBmp = makeSrc(width, height);
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.drawBitmap(dstBmp, 0,0 , mPaint);
        /*
        ADD,
        CLEAR,
        DARKEN,
        DST,
        DST_ATOP,
        DST_IN,
        DST_OUT,
        DST_OVER,
        LIGHTEN,
        MULTIPLY,
        OVERLAY,
        SCREEN,
        SRC,
        SRC_ATOP,
        SRC_IN,
        SRC_OUT,
        SRC_OVER,
        XOR;*/
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
        canvas.drawBitmap(srcBmp, width / 2, height / 2, mPaint);
        mPaint.setXfermode(null);
        canvas.restore();
    }

    private Bitmap makeDst(int w, int h) {
        Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setColor(0xFFFFCC44);
        c.drawOval(new RectF(0, 0, w, h), p);
        return bm;
    }

    private Bitmap makeSrc(int w, int h) {
        Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setColor(0xFF66AAFF);
        c.drawRect(0, 0, w, h, p);
        return bm;
    }
}
<com.loaderman.customviewdemo.view.PorterDuffXfermodeView
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

效果:

技术分享图片

以上是关于PorterDuffXfermode之PorterDuff.Mode.XOR入门学习的主要内容,如果未能解决你的问题,请参考以下文章

PorterDuffXfermode之PorterDuff.Mode.MULTIPLY

PorterDuffXfermode之PorterDuff.Mode.XOR入门学习

PorterDuffXfermode之Mode.SRC_IN

PorterDuffXfermode之PorterDuff.Mode.DST_IN

Android Paint之 setXfermode PorterDuffXfermode 讲解

随行付数据同步中间件「Porter」开源啦