I'm trying to write a graph class I can use in Android(I'm aware pre-made ones exist), but converting all of my coordinates would be a pain. Is there an easy way to make the screen coordinates start at the bottom left?
Asked
Active
Viewed 1.0k times
7
-
All of the comments so far address this in different ways (creative!) I would think you would still want to add more context to your question, like why and what you then expect "left" and "up" to be at that point – Idistic Jul 13 '11 at 16:57
-
I agree with the above, it's slightly vague, so for reference: The accepted answer creates a coordinate system with its origin in the bottom left corner of the screen, with domain X = [0, `canvas.getWidth()`), Y = [0, `canvas.getHeight()`) – LeffelMania Jul 13 '11 at 17:32
4 Answers
16
No, I don't know of a way to move 0,0
to the bottom left and get what you would typically think of as "normal" coordinates.
But combining scale()
and translate()
might do the trick to achieve the same effect.
canvas.translate(0,canvas.getHeight()); // reset where 0,0 is located
canvas.scale(1,-1); // invert

dustmachine
- 10,622
- 5
- 26
- 28
-
-
1The translation needs to come before the scale. Since you're scaling first, the translation is shifting the canvas the opposite direction that you intend. Also remember to call `canvas.save()` before the transformation and `canvas.restore()` after you've completed drawing. – LeffelMania Jul 13 '11 at 17:02
-
The problem was you need to call translate() before scale(), works now. – John Lotacs Jul 13 '11 at 17:04
-
-
I wonder why do almost all code samples contain, and why do so many people suggest canvas.save() and canvas.restore(). If I omit them the code works the same, Android still seems to do these before/after onDraw() – P Varga Sep 03 '12 at 21:13
-
3
-
1Yeah, about that upside-down text. Calling canvas.scale(1, 1) before drawing text and calling canvas.scale(1,-1) when done does not fix the problem. Neither does using canvas.save() and canvas.restore(). Anybody have an answer to the text-upside down problem? – AlanKley May 29 '18 at 18:30
-
Note: this lines need to be placed before any actual rendering. – Boris Strandjev Feb 11 '20 at 17:11
1
You can flip your Canvas with something like canvas.scale(1, -1)
and then translate it to the right place.

Guillaume Brunerie
- 4,676
- 3
- 24
- 32
0
The android canvas has the origin at the top left. You want to translate this to the bottom right. To do this translation, subtract the y co-ordinate from the Canvas height.
float X1 = xStart;
float Y1 = canvas.getHeight() - yStart; //canvas is a Canvas object
float X2 = xEnd;
float Y2 = canvas.getHeight() - yEnd;
canvas.drawLine(X1, Y1, X2, Y2, paint ); //paint is a Paint object
This should make your line start from the bottom left.

CanonicalBear
- 367
- 8
- 12
0
You can use canvas.translate()
http://developer.android.com/reference/android/graphics/Canvas.html#translate(float, float) to move the origin to where you want.

palacsint
- 28,416
- 10
- 82
- 109

André Oriani
- 3,553
- 22
- 29