Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using setScale and setTranslate (Matrix)

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

like image 605
Paramount Avatar asked Mar 19 '11 05:03

Paramount


2 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.

like image 114
Romain Guy Avatar answered Nov 08 '22 20:11

Romain Guy


Response code Romain

matrix.setScale(zoom, zoom); // this will not work
matrix.postTranslate(currentX, currentY); // this will work
canvas.drawBitmap(image, matrix, null);
like image 32
Omar Avatar answered Nov 08 '22 20:11

Omar