android图形图像处理PorterDuffXfermode

设置两张图片重叠的模式。
在正常的情况下,在已有的图像上绘图将会在其上面添加一层新的形状。如果新的Paint是完全不透明的,那么它将完全遮挡住下面的Paint;如果它是部分透明的,那么它将会被染上下面的颜色。
而setXfermode就可以来解决这个问题 .
android图形图像处理PorterDuffXfermode_第1张图片
举例android图形图像处理PorterDuffXfermode_第2张图片

重叠的图层使用BitmapCanvas画布。。注意下面

public class MyBitmapView2 extends View {
    private int width;
    private int height;

    private Paint paintCircle;
    private Paint paintRect;

    private Bitmap bitmap;
    private Canvas bitmapCanvas;

    public MyBitmapView2(Context context, AttributeSet attrs) {
        super(context, attrs);
        paintCircle = new Paint();
        paintRect = new Paint();
        paintRect.setColor(Color.GREEN);
        paintCircle.setColor(Color.YELLOW);

        //设置画笔图层在上
        PorterDuffXfermode mode = new PorterDuffXfermode(PorterDuff.Mode.DST_OVER);
        paintRect.setXfermode(mode);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        setMeasuredDimension(width, height);


        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmapCanvas = new Canvas(bitmap);//bitmap画布
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        canvas.drawColor(Color.RED);
        bitmapCanvas.drawCircle(width / 2, height / 2, width / 2, paintCircle);
        bitmapCanvas.drawRect(0, 0, width / 2, width / 2, paintRect);

        canvas.drawBitmap(bitmap, 0, 0, null);

    }


}

你可能感兴趣的:(android,canvas,bitmap,图像处理)