MarcTheSpark
MarcTheSpark

Reputation: 543

How to draw an outlined path on android canvas

Is there a good way of drawing a path (or even just a straight line) on an android canvas where the line is in one color, but outlined with another? Here's what I'm looking to draw:

Outlined Path

...the purpose of course being that the line (in my case a dashed line) is easily visible on both a black and white background.

Thanks!

Upvotes: 1

Views: 2173

Answers (2)

Carlos Robles
Carlos Robles

Reputation: 10947

You should first draw a thicker line with the color of the border, over it, you draw another line, at a distance of 1px to every side of the other line, so it will cover the first line, but 1 keeping visible 1px around the second line. something like this:

  public void onDraw(Canvas canvas) {

            float startX, startY, stopX, stopY;//remenber to inicialize them with actual values
            int  BORDER_COLOR, INNER_COLOR;//remenber to inicialize them with actual values

            Paint paint = new Paint();

            paint.setColor( BORDER_COLOR);
            canvas.drawLine(startX, startY, stopX, stopY, paint);

            paint.setColor( INNER_COLOR);
            canvas.drawLine(startX+1, startY+1, stopX-1, stopY-1, paint);

    }

Upvotes: 1

pskink
pskink

Reputation: 24720

draw twice: with different color and different stroke width

Upvotes: 1

Related Questions