创建一个Bitmap的canvas,然后用这个canvas对象画两个相互交叠的图形,使用上面图形的画笔调用setXfermode方法添加PorterDuffXfermode对象。
PorterDuffXfermode有如下图16种显示
使用mode.XOR示例
/**
* Created by Administrator on 2015/9/17.
*/
public class BitMapView2 extends View{
private int width;
private int height;
private Bitmap bitmap;
private Paint mPaint;
private Canvas canvasBit;
private Paint mPaintRect;
private Paint mPaintCircle;
public BitMapView2(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintRect=new Paint();
mPaintRect.setAntiAlias(true);
mPaintRect.setStyle(Paint.Style.FILL);
mPaintRect.setColor(Color.YELLOW);
mPaintCircle=new Paint();
mPaintCircle.setColor(Color.BLUE);
mPaintCircle.setAntiAlias(true);
mPaintCircle.setStyle(Paint.Style.FILL);
PorterDuffXfermode mode=new PorterDuffXfermode(PorterDuff.Mode.XOR);//【3】设置mode重叠透明
mPaintRect.setXfermode(mode);//矩形在上面
}
public BitMapView2(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width= getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
height=getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
bitmap=Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);//创建Bitmap
canvasBit=new Canvas(bitmap) // 【1】创建bitmap的Canvas
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);
canvasBit.drawCircle(width / 2, height / 2, width / 2, mPaintCircle);//【2】在bitmap的Canvas上画两个重叠图形
canvasBit.drawRect(0,0,width/2,width/2,mPaintRect);
canvas.drawBitmap(bitmap,0,0,null);
}
}