Android 了解ImageView矩阵的用法

Android 了解ImageView矩阵的用法,android,image,matrix,rotation,image-rotation,Android,Image,Matrix,Rotation,Image Rotation,我知道SO充满了矩阵问题,但我找不到一个问题能得到充分的解释。我猜任何ImageView都有一个矩阵,负责缩放、旋转和位置。 但是为什么我不能使用这样的矩阵旋转图像: ImageView img = (ImageView)findViewById(R.id.some_imageview); img.setScaleType(ScaleType.Matrix); Rect bounds = img.getDrawable.getBounds(); img.getImageMatrix().post

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

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);
有几个答案建议这样做:

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);
为什么每次旋转时都要创建一个新的矩阵?此外,如果我设置第二个示例中的矩阵,如果我再次设置
rotationMatrix
,为什么它不会再次旋转(到其原始程度)?如果我想得到原始度,我可以设置一个简单构造的矩阵。但我还是不明白为什么

img.getImageMatrix().postRotate(180f, bounds.width() / 2, bounds.height() / 2); 
这是行不通的

注意:我还尝试了
setRotate
方法,没有观察到任何差异

编辑:由于评论

我问过为什么每次我都要创建一个新的矩阵,这就意味着一个问题,为什么我不能在适当的地方使用矩阵。 我还怀疑这是可行的(实际上也不会):


在setImageMatrix()方法中,应用了矩阵计算。如果使用setter方法进行计算,则事后使用此矩阵(即通过getter获取后)将不会产生任何效果。

文档说明不应使用getImageMatrix返回的矩阵。看一看@blackbelt我要求解释为什么我不应该在适当的位置使用这个矩阵,至少如果它不是空的…,特别是如果我之前自己设置了它…实际上你问“为什么每次我要旋转时都要创建一个新的矩阵?”@blackbelt你是对的,重构bitI我认为您不应该更改从getImageMatrix获得的矩阵,而是应该使用自己的矩阵(并且可以只初始化一次)。您可以扩展imageView并拥有一个允许修改的矩阵,或者为您所在的任何类使用矩阵字段。