I know that SO is full of Matrix questions, but I cannot find a question where this is fully explained. I assume that any ImageView has a matrix that is responsible for scaling, rotation and position. But why can't I rotate the image using the 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 doing this as follows:
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 do I have to create a new matrix every time I want to rotate? Also, if I install the Matrix from the second example, why doesn't it rotate again (to its original degree) if I install rotationMatrix again? If I want to get an initial degree, I can set a simple matrix. but again, I donโt understand why
img.getImageMatrix().postRotate(180f, bounds.width() / 2, bounds.height() / 2);
will not work.
Note. I also tried the setRotate method without any differences
EDIT: due to comment
I asked why I have to create a new matrix every time, which implies the question of why I cannot use the Matrix in place. I also suspect that it was (which actually won't happen either):
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);
android matrix image rotation image-rotation
Rafael t
source share