I make an Application in which I want to erase drawing lines with event. For this I used
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
but at the time of erasing a line, that line becomes black first then erased. I want a transparent color for erasing a drawing a path.
I have gone through the FingerPaint.java
from APIDemos i.e android-sdk\samples\android-17\ApiDemos
and modified
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
to
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
mCanvas.drawPath(mPath, mPaint); // this line changed
// mCanvas is Canvas variable which is
// initialized in onSizeChanged()
}
Now it is not drawing a black color while erasing, everything works fine. Not sure it is 100% proper answer but it works for me.
Hey I have used a kind of trick to remove the black line.In my erase button, I have set the color to white, instead of using XferMode..
if(erase){
paintColor = Color.parseColor(newColor);
drawPaint.setColor(paintColor);
}
The below works for erasing on a transparent background...
Call SetErase(true) to start erasing.
The OnDraw method will then draw a white path (instead of black)which will then get cleared to the transparent color and you keep all your path undo info.
Call SetErase() to toggle erasing on/off
public void SetErase(bool On)
{
if (On)
{
if (!_erasing)
{
_delpaint = new Paint(_paint);
_delpaint.Color = Color.White;
_paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.Clear));
_erasing = true;
}
}
else if (_erasing)
{
_erasing = false;
_paint.SetXfermode(null);
}
}
protected override void OnDraw(Canvas canvas)
{
canvas.DrawColor(BackgroundColor);
canvas.DrawBitmap(CanvasBitmap, 0, 0, _bitmapPaint);
if (_erasing)
{
canvas.DrawPath(_path, _delpaint); // draw white path
}
else
{
canvas.DrawPath(_path, _paint);
}
}