使用 Android Paint 进行背景图像颜色检测

Background image color detection with Android Paint

当我开始绘画时,它为整个背景着色,我的意思是它应该只绘制白点。 应用截图如下。 使用 Android Paint ,我只想在 background-drawable[Panda] 上绘制白色斑点并跳过任何其他颜色。

onDraw()函数为:

protected void onDraw(Canvas canvas) {

    canvas.drawPath(path, paint);
    canvas.drawPath(circlePath, circlePaint);

    for (Pair<Path,Integer> path_clr : path_color_list ){
        paint.setColor(path_clr.second);
        canvas.drawPath( path_clr.first, paint);
    }

    for (Pair<Path,Integer> path_clr : circular_path_color_list ){
        circlePaint.setColor(path_clr.second);
        canvas.drawPath( path_clr.first, paint);
    }
}

onTouchEvent函数是:

public boolean onTouchEvent(MotionEvent event) {

    float pointX = event.getX();
    float pointY = event.getY();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        circlePath.reset();
        path.moveTo(pointX, pointY);

        return true;
    case MotionEvent.ACTION_MOVE:
        path.lineTo(pointX, pointY);
        circlePath.reset();
        circlePath.addCircle(pointX, pointY, 10, Path.Direction.CW);

        break;

    case MotionEvent.ACTION_UP:
        circlePath.reset();

        break;
    default:
        return false;
    }

    postInvalidate();
    return true;
}

你描述的东西叫做掩蔽。您需要一个遮罩(白色区域)和一个遮罩图像(您的笔划)。绘图时,您必须使用蒙版将笔画切割成蒙版的形状。它可以使用 PorterDuff 模式来完成。见伪代码:

Bitmap panda;
Bitmap whiteAreas;
Bitmap strokes;
Canvas strokesCanvas;
Paint paint;

private void init() {
    strokesCanvas = new Canvas(strokes);
    paint = new Paint();
}

private void addStroke(Path stroke){
    paint.setXfermode(null);
    strokesCanvas.drawPath(stroke,paint);
    invalidate();
}

@Override
public void draw(Canvas canvas) {
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    strokesCanvas.drawBitmap(whiteAreas,0,0,paint);
    paint.setXfermode(null);
    canvas.drawBitmap(panda,0,0,paint);
    canvas.drawBitmap(strokes,0,0,paint);
}

有关详细信息,请参阅 link:http://ssp.impulsetrain.com/porterduff.html


编辑:这是它如何工作的图像。蓝色区域应该是透明的。掩码和笔画之间的乘法就是所谓的掩码。