How to get the current translation position from the canvas? I am trying to draw material where my coordinates are a mixture of relative (to each other) and absolute (to canvas).
Suppose I want to do
canvas.translate(x1, y1); canvas.drawSomething(0, 0); // will show up at (x1, y1), all good // now i want to draw a point at x2,y2 canvas.translate(x2, y2); canvas.drawSomething(0, 0); // will show up at (x1+x2, y1+y2) // i could do canvas.drawSomething(-x1, -y1); // but i don't always know those coords
This works, but dirty:
private static Point getCurrentTranslate(Canvas canvas) { float [] pos = new float [2]; canvas.getMatrix().mapPoints(pos); return new Point((int)pos[0], (int)pos[1]); } ... Point p = getCurrentTranslate(canvas); canvas.drawSomething(-px, -py);
The canvas has a getMatrix method, it has setTranslate , but not getTranslate . I don't want to use canvas.save() and canvas.restore() , because the way of drawing things is a bit complicated (and probably messy ...)
Is there a cleaner way to get these current coordinates?
android canvas
kristianlm
source share