Android Question How to draw transparent (alpha) line without buildup?

Discussion in 'Android Questions' started by Dave O, May 28, 2015.

  1. Dave O

    Dave O Active Member Licensed User

    Hi all,

    Short version:
    Is there a way to draw a transparent line so that it doesn't overlap/build up when drawn over itself? (That is, so that I can draw an alpha=50 line and draw over it again and it's still alpha=50 (instead of getting darker at the intersections)?

    Longer version:
    I'm drawing a semi-transparent line (e.g. yellow, alpha = 50) based on touch events, and that works well, except that I'm drawing a line from touch point to touch point, with a circle at each junction to keep the line "solid" with no ugly elbows.

    This works fine for opaque lines, but for transparent lines, I get an ugly overlap because the alpha is cumulative.

    Ideally, I'd like to draw the line so that its alpha is always 50, no matter how many times I draw over it.

    Is there some kind of blend mode (or other graphics voodoo) that would help?

    Or perhaps I have to draw the line opaque, but wrapped somehow inside a transparent drawable?

    OK, now my brain hurts... :)
  2. Erel

    Erel Administrator Staff Member Licensed User

    Do you make the drawings over a bitmap that was previously drawn with Canvas.DrawBitmap?
  3. Dave O

    Dave O Active Member Licensed User

    Yes, I'm drawing lines on a canvas assigned to a panel.

    My typical use for alpha drawing is to be a semi-transparent highlighter drawing over other (usually black) shapes previously drawn on the same canvas.

    Attached is an example showing the yellow alpha pen drawing over a black shape on the left, then by itself on the right. Ideally, the yellow would be smooth (all alpha 50) instead of blotchy because of the overlapping of the line segments.

    Attached Files:

  4. klaus

    klaus Expert Licensed User

    Couldn't you draw the highlighter on another transparent Panel using it as a separate overlaying layer ?
    Erel likes this.
  5. Dave O

    Dave O Active Member Licensed User

    Yes, I could, but I'm not sure how that would change the ultimate rendering of the alpha pen.

    Or do you mean drawing the yellow as opaque (which makes it all the same color value because alpha is maxed out), and making that whole layer somehow alpha-transparent?
  6. thedesolatesoul

    thedesolatesoul Expert Licensed User

  7. klaus

    klaus Expert Licensed User

    It was just a suggestion, I haven't tested it.
  8. Dave O

    Dave O Active Member Licensed User

    @thedesolatesoul : Well, I'm already using the ABExtDrawing library to draw a transparent rounded rectangle, so it should be easy for me to play around with the Porter-Duff modes to see if any of them do what I want. Kind of like the first time I used Photoshop and spent an hour just playing with the drawing modes. :)
    thedesolatesoul likes this.
  9. Erel

    Erel Administrator Staff Member Licensed User

    If you are using a second layer then you can erase the relevant area before drawing over it.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice