Home » Android » java – Using setScale and setTranslate (Matrix)

java – Using setScale and setTranslate (Matrix)

Posted by: admin June 15, 2020 Leave a comment

Questions:

In my Android app I have an image that loads in. With this image the user can zoom in, out, and move it back and forth. Currently I can only get one to work at a time.

After a lot of testing I have determined that whatever I call second is the one that works.

matrix.setScale(zoom, zoom); // this will not work
matrix.setTranslate(currentX, currentY); // this will work
canvas.drawBitmap(image, matrix, null);

If I ran all the same code but simply switched setScale second it would then work but setTranslate wont.

This seems like it should be a simple answer.
BTW: with the way my code is set up using post will not be practical.

matrix.postScale();
matrix.postTranslate(); 

Thanks in advance

How to&Answers:

When you call any of the set*() method you replace the entire content of the Matrix. In your first example, only setTranslate() is taken into account. You need to use the pre*() and post*() methods to combine the translate and scale operations.

Answer:

Response code Romain

matrix.setScale(zoom, zoom); // this will not work
matrix.postTranslate(currentX, currentY); // this will work
canvas.drawBitmap(image, matrix, null);