How to find the current translation position in Canvas?

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?

+8
android canvas
source share
1 answer

First you need to convert the reset transformation matrix. I'm not an Android developer, looking at android canvas documents , there is no reset matrix, but there is setMatrix (android. Graphics.Matrix). He says that if the given matrix is ​​zero, it will set the current matrix to the single matrix that you need. Therefore, I think you can reset your position (both scale and skew) with:

 canvas.setMatrix(null); 

You can also get the current translation through getMatrix. There is a mapVectors () method that you can use for matrices to see where the point [0,0] will be displayed, this will be your translation. But in your case, I believe that the best option is to reset the matrix.

0
source share

All Articles