Using setScale and setTranslate (Matrix)

In my Android app, I have an image that is loading. With this image, the user can enlarge, delete and move it forward and backward. Currently, I can make it work at the same time.

After much testing, I determined that all I call the 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 run all the same code, but just switched setScale, then it will work, but setTranslate will not.

It seems to be a simple answer. BTW: with the way my code is configured using the message will not be practical.

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

Thanks in advance

+7
source share
2 answers

When you call any of the * () methods, you replace the entire contents of the Matrix. The first example only considers setTranslate (). You need to use the pre * () and post * () methods to combine translation and scaling operations.

+18
source

Answer Code Romain

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

All Articles