1

I'm trying to draw a rectangle where the lines are two pixels wide in Android. The code looks something like:

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(2);
    canvas.drawRect(left, top, right, bottom, paint);

In the emulator the rightmost line starts at right and then uses one more pixel, the leftmost pixel starts at left and uses one more pixel. In other words the extra pixel for the stroke width is rendered to the right of both lines. With a real device the rightmost line starts at right-1, in other words the extra stroke width is rendered inside the rectangle for all lines.

I haven't tested with other real devices, but I need to know what happens. A simple workaround is to use two drawRect calls with stroke width 1, but that is inefficient.

Is this behaviour undefined (so that I to use a workaround in order to be portable) or is there a way to control it?

EDIT: answers to comments - the emulator and the device have the same screen configuration. I saw the other post and while similar it is not an exact duplicate? Or is the answer that there is no portable way to use stroke width 2 if it is important where the pixels are (say if I'm drawing one rectangle inside another one and need the same margins)?

ewramner
  • 5,810
  • 2
  • 17
  • 33

0 Answers0