To my surprise I\'ve just discovered that drawLine and drawRect don\'t include the ending position, i.e.:
canvas.drawLine(100, 100, 100, 100, paint);
Your question is essentially the answer for this. Thank you.
I, for one, am not surprised by this, and wouldn't want it any other way. Here's why:
It is consistent with java.awt
, javax.swing
and other APIs, as well as core Java methods such as String.substring(int, int)
and List>.get(int)
.
It is compatible with android.graphics.RectF
, which doesn't care about pixels—how can you have a fraction of a pixel?
The API is convenient:
For a 40×30 rectangle, instantiate Rect(0, 0, 40, 30)
Rect.right - Rect.left == Rect.width()
The maths and geometry are easier this way. For example, if you want draw a rectangle centred within the canvas:
Rect clipBounds = canvas.getClipBounds() int myRectWidth = 20; int myRectHeight = 10; int left = (clipBounds.width() - myRectWidth) / 2; int top = (clipBounds.height() - myRectHeight) / 2; int right = clipBounds.width() - left; // See how nice this is? int bottom = clipBounds.height() - top; // This wouldn't work if bottom was inclusive! canvas.drawRect(left, top, right, bottom);
The API is right; the API documentation, unfortunately, just lacks detail.
(Actually, it is mentioned in the detail for the Rect.contains(int, int) method. It's unfortunate that Google let the API out the door without the field variables themselves being documented, however.)