Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

understanding usage of ImageView Matrix

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);
like image 790
Rafael T Avatar asked Jul 12 '13 13:07

Rafael T


People also ask

What is the purpose of the image view?

ImageView class is used to display any kind of image resource in the android application either it can be android.

What is the use of matrix in 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.

What is the use of set image resource?

setImageResource(): Use a resource id to set the content of the ImageView. setImageUri(): Use a URI to set the content of the ImageView.


1 Answers

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.

like image 162
mad_manny Avatar answered Sep 24 '22 04:09

mad_manny