了解ImageView Matrix的用法

时间:2013-07-12 13:00:48

标签: android image matrix rotation 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);

几个答案建议这样做:

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);

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

img.getImageMatrix().postRotate(180f, bounds.width() / 2, bounds.height() / 2); 

不起作用。

注意:我也尝试了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);

1 个答案:

答案 0 :(得分:1)

在setImageMatrix()方法中查看它看起来像source,应用矩阵计算。之后使用此矩阵(即在获取者获得之后)如果在setter方法中完成计算,则不会产生任何影响。

相关问题