Pregunta

I know that SO is full of Matrix questions, but I can't find a question where it is fully explained. I guess that any ImageView has a Matrix which is responsible for scaling, rotating and the position. But why I can't rotate an Image using a Matrix like this:

ImageView img = (ImageView)findViewById(R.id.some_imageview);
img.setScaleType(ScaleType.Matrix);
Rect bounds = img.getDrawable.getBounds();
img.getImageMatrix().postRotate(180f, bounds.width() / 2, bounds.height() / 2);

several answers suggest to do it like this:

ImageView img = (ImageView)findViewById(R.id.some_imageview);
img.setScaleType(ScaleType.Matrix);
Rect bounds = img.getDrawable.getBounds();
Matrix rotationMatrix = new Matrix();
rotationMatrix.postRotate(180f, bounds.width() / 2, bounds.height() / 2);
img.setImageMatrix(rotationMatrix);

WHY I have to create a new Matrix every time I want to rotate? Furthermore, If I set the Matrix from the second example, Why it isn't rotating again (to its original degree) if I set the rotationMatrix again? If I want to get the original degree I can set a plain constructed Matrix. but Again, I do NOT understand why

img.getImageMatrix().postRotate(180f, bounds.width() / 2, bounds.height() / 2); 

will not work.

Note: I have also tried the setRotate method without observing any difference

EDIT: due to a comment

I have asked Why I have to create a new Matrix everytime, which implies the question, why I cannot use the Matrix in place. Also What I suspect to work was this (which actually won't, too):

ImageView img = (ImageView)findViewById(R.id.some_imageview);
img.setScaleType(ScaleType.Matrix);
Rect bounds = img.getDrawable.getBounds();
Matrix rotationMatrix = new Matrix();
rotationMatrix.postRotate(180f, bounds.width() / 2, bounds.height() / 2);
img.setImageMatrix(rotationMatrix);
//works until here. 

//Then after that successful call

 //assumed to get my Matrix back, which is rotated by 180 degrees

 Matrix matrix = img.getImageMatrix();
 Rext bounds = img.getDrawable().getBounds()
 //rotate again at 90 degree. It should be now rotated 270 degrees (180 from before, plus 90 now)
 matrix.postRotate(90f, bounds.width() / 2, bounds.height() / 2);
 //unfortunately NO effect!
 img.setImageMatrix(matrix);
¿Fue útil?

Solución

Looking at the source it looks like in the setImageMatrix() method the matrix calculation is applied. Using this matrix afterwards (i.e. after obtaining it by the getter) won't have any effect if the calculation is done in the setter method.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top