Android画布用画布绘制

时间:2016-03-18 20:58:08

标签: android bitmap android-canvas mask

我尝试通过在画布上绘制线条来创建蒙版。但是,当我完成图像时,它只是原始图像。如果我发送mask位图,它会按预期显示,并且原始图像是正确的。如何在两个位图之间进行屏蔽?

@Override
public void onDraw(Canvas canvas) {
    if (lines == null) {
        return;
    }
    Bitmap mask = null;
    if(performMask) {
        mask = Bitmap.createBitmap((int)getWidth(), (int)getHeight(), Bitmap.Config.RGB_565);
        canvas = new Canvas(mask);
    }

    for (int i = 0; i < lines.size(); i++) {
        ArrayList<Point> line = lines.get(i);
        for (int j = 1; j < line.size(); j++) {
            Point start = line.get(j - 1);
            Point end = line.get(j);
            canvas.drawLine(start.x, start.y, end.x, end.y, drawingPaint);
        }
    }

    if(performMask) {
        performMask = false;

        Bitmap result = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas tempCanvas = new Canvas(result);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        tempCanvas.drawBitmap(original, 0, 0, null);
        tempCanvas.drawBitmap(mask, 0, 0, paint);
        paint.setXfermode(null);
        if(maskPerformedListener != null) {
            maskPerformedListener.onMaskCompleted(result);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我需要更改此行

mask = Bitmap.createBitmap((int)getWidth(), (int)getHeight(), Bitmap.Config.RGB_565);

mask = Bitmap.createBitmap((int)getWidth(), (int)getHeight(), Bitmap.Config.RGB_8888);

原来你需要alpha层来实现这一点。