了解ImageView Matrix的用法

Raf*_*l T 7 android image rotation matrix image-rotation

我知道SO充满了Matrix问题,但我找不到一个完全解释的问题.我猜任何ImageView都有一个Matrix,负责缩放,旋转和位置.但为什么我不能像这样使用矩阵旋转图像:

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);
Run Code Online (Sandbox Code Playgroud)

几个答案建议像这样做:

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);
Run Code Online (Sandbox Code Playgroud)

为什么每次我想要旋转时都必须创建一个新的矩阵?此外,如果我从第二个例子设置矩阵,如果我再次设置它为什么它不再旋转(到原始程度)rotationMatrix?如果我想获得原始学位,我可以设置一个简单构造的矩阵.但同样,我不明白为什么

img.getImageMatrix().postRotate(180f, bounds.width() / 2, bounds.height() / 2); 
Run Code Online (Sandbox Code Playgroud)

不管用.

注意:我也尝试过这种setRotate方法,但没有观察到任何差异

编辑:由于评论

我问过为什么每次都要创建一个新的Matrix,这意味着问题,为什么我不能使用Matrix.我怀疑工作的是这个(实际上也不会):

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);
Run Code Online (Sandbox Code Playgroud)

mad*_*nny 2

查看源代码,它看起来像是在 setImageMatrix() 方法中应用了矩阵计算。如果计算是在 setter 方法中完成的,那么之后(即通过 getter 获取它之后)使用该矩阵不会有任何效果。