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

时间:2015-01-06 08:29:17

标签: android paint android-drawable

当我开始绘画时,它会为整个背景着色,我的意思是它应该只绘制白点。 应用程序截图如下。 使用Android Paint,我想在背景可绘制的[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;
}

Color Activity

1 个答案:

答案 0 :(得分:6)

你所描述的东西叫做掩蔽。你需要一个面具(白色区域)和一个蒙面图像(你的笔画)。绘图时,您必须使用蒙版将笔划剪切为蒙版的形状。它可以使用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);
}

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


编辑:这是一张图片如何运作。蓝色区域应该是透明的。掩码和笔画之间的乘法就是所谓的掩蔽。

enter image description here