How to change the default erase color in a drawing application in Android?

I am making an application in which I want to delete drawing lines using an event. For this I used

mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 

but while erasing a line this line becomes black and then erased. I want a transparent color to erase the picture.

+6
source share
3 answers

I went through FingerPaint.java from APIDemos ie 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 he does not paint black when erased, everything works fine. Not sure if this is a 100% correct answer, but it works for me.

+1
source

Hey, I used a trick to remove the black line. In my erase button, I set the color to white instead of using XferMode ..

 if(erase){ paintColor = Color.parseColor(newColor); drawPaint.setColor(paintColor); } 
0
source

The following is erasing on a transparent background ...

Call SetErase (true) to start erasing.

Then, the OnDraw method will draw a white path (instead of black), which will then be cleared to a transparent color, and you will save all the information about canceling the path.

Call SetErase () to toggle 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); } } 
0
source

Source: https://habr.com/ru/post/912020/


All Articles