18

I am trying to "erase" from a canvas. Just as a VERY simple test of clearing a canvas, I have implemented the following:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.argb(140, 0, 0, 0));
    canvas.drawColor(0, Mode.CLEAR);
}

Color.argb(140, 0, 0, 0) sets the view this view is drawn over to be dimmed. drawColor(0, Mode.CLEAR) makes the screen completely black, rather than removing the dimming applied earlier. The idea was taken from here

Community
  • 1
  • 1
rperryng
  • 3,233
  • 3
  • 22
  • 35
  • I'm pretty sure that `canvas.drawColor(0, Mode.CLEAR);` making the screen completely black is the intended functionality, so it's not really clear what you are asking/trying to accomplish - because you are accomplishing it. If what you are trying to do is just temporarily dim a view/shade a bitmap, then remove the dimming effect, that's not really "clearing" the canvas. – Submersed Feb 18 '14 at 21:12
  • Well, what I was trying to accomplish was to set a custom view to overlay an activity, that custom view would fill the screen with a low-alpha black to provide a dimming pattern, and then I would want a circle somewhere on the screen that did NOT have the dimming pattern – rperryng Feb 18 '14 at 21:35

3 Answers3

27

Use the following.

 canvas.drawColor(Color.TRANSPARENT,Mode.CLEAR);
Y0Gi
  • 423
  • 4
  • 10
  • That's already what I have in my code, since Color.TRANSPARENT is a constant for 0. I have already found a solution though, thank you. – rperryng May 14 '15 at 17:33
  • This single line solution is much better than whatever double Canvas/Bitmap nonsense is going on in the accepted answer. –  Oct 23 '15 at 20:47
  • As Michael Herbig says this should be the answer. Using a Bitmap will use twice as much memory and cause another level of buffering! – deive Oct 27 '15 at 13:49
  • As stated in my original comment, I already had this exact line in my original code (see OP), however it did not work for me at the time, 3 years later it seems this has helped more people than my own own, so I will mark this as the correct answer – rperryng Jul 12 '17 at 07:53
  • 22
    Doing this, my Canvas becomes totally black – Javier Delgado Jul 25 '17 at 14:50
  • How to make it transparent? – Evan Apr 23 '18 at 03:10
13

The solution was to create a secondary canvas and bitmap to draw on. My Custom View's onSizeChanged() method looked like

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
    bitmap.eraseColor(Color.TRANSPARENT);
    temp = new Canvas(bitmap);
}

and the onDrawMethod looks like

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    temp.drawColor(Color.argb(80, 0, 0, 0));
    temp.drawCircle(centerPosX, centerPosY, 200, transparentPaint);
    canvas.drawBitmap(bitmap, 0, 0, null);
}

where transparentPaint is declared in the onstructor as

transparentPaint = new Paint();
transparentPaint.setColor(getResources().getColor(android.R.color.transparent));
transparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
transparentPaint.setAntiAlias(true);
rperryng
  • 3,233
  • 3
  • 22
  • 35
-1

You need to set the whole view to transparent, otherwise, the transparent color will be black.

At the constructor of the view that you are overwriting set the alpha to zero.

setAlpha(0); // this is the secret.

Then, at you onDraw() method, you can clear the view:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // ...
    canvas.drawColor(Color.TRANSPARENT,Mode.CLEAR);
    // do you painting here
}
Derzu
  • 7,011
  • 3
  • 57
  • 60