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);
ImageView class is used to display any kind of image resource in the android application either it can be android.
Just think of a matrix as an array of numbers. In this case, an Android Matrix has 3 rows of 3 numbers. Each number tells an Android graphics function what to do to scale (bigger/smaller), translate (move), rotate (turn) or skew (distort in a 2D plane) the "thing" which the matrix is applied to.
setImageResource(): Use a resource id to set the content of the ImageView. setImageUri(): Use a URI to set the content of the ImageView.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With