John Lotacs
John Lotacs

Reputation: 1254

Android: Drawing to canvas, way to make bottom left correspond to (0,0)?

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?

Upvotes: 8

Views: 10706

Answers (4)

CanonicalBear
CanonicalBear

Reputation: 397

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.

Upvotes: 0

André Oriani
André Oriani

Reputation: 3613

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.

Upvotes: 0

dustmachine
dustmachine

Reputation: 10824

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

Upvotes: 17

Guillaume Brunerie
Guillaume Brunerie

Reputation: 5371

You can flip your Canvas with something like canvas.scale(1, -1) and then translate it to the right place.

Upvotes: 1

Related Questions