9

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);
Rafael T
  • 15,401
  • 15
  • 83
  • 144
  • the documentation says that you should not use the matrix returned by getImageMatrix. Take a look [here](http://developer.android.com/reference/android/widget/ImageView.html#getImageMatrix()) – Blackbelt Jul 12 '13 at 13:05
  • @blackbelt I asked for a explanation WHY I shouldn't use this Matrix in place, at least if it is not null..., especially If i set it myself before... – Rafael T Jul 12 '13 at 13:20
  • 1
    Actualy you asked "WHY I have to create a new Matrix every time I want to rotate?" – Blackbelt Jul 12 '13 at 13:22
  • @blackbelt you're right, refrased a bit – Rafael T Jul 12 '13 at 13:28
  • I think that you don't supposed to change the matrix you get from getImageMatrix , but instead you should use your own (and can initialize it just once). you could extend imageView and have there a matrix that is allowed to be modified , or use a field of the matrix for whatever class you are in. – android developer Jan 06 '14 at 12:43

1 Answers1

2

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.

mad_manny
  • 1,081
  • 16
  • 28