25

I want to rotate an image using ImageView.setImageMatrix(matrix) but it simply doesn't have any effect. I call matrix.postRotate(45, 20, 20); before passing it to the function above but no effect, the image is not rotated. Why?

matrix.postRotate(45, 20, 20);
ImageView.setImageMatrix(matrix)

1 Answer 1

76

I'm not really sure but I think you should set scale type:

imgView.setScaleType(ScaleType.MATRIX);

You can also do that in XML with android:scaleType="matrix".

5
  • @Mr. Roland: oh good. I was not sure because recently I looked at ImageView source and found that mMatrix (the image matrix) is read also in other modes, but I looked again now and actually it is reset by using the Matrix.set*() series of methods.
    – bigstones
    Commented Jan 24, 2011 at 16:48
  • 5
    It doesn't work for me. My code Matrix matrix = new Matrix(); matrix.postRotate(90); my_img.setScaleType(ScaleType.MATRIX); my_img.setImageMatrix(matrix); and nothing happens!
    – thomaus
    Commented Mar 19, 2013 at 18:12
  • 2
    You need to use matrix.postRotate(90, imageWidth / 2, imageHeigth / 2);
    – user4377163
    Commented Dec 19, 2014 at 8:25
  • 2
    Setting the drawable AFTER setting the matrix fixed the issue for me
    – TheIT
    Commented Jul 20, 2017 at 21:43
  • Thank you, I was stuck with this for a long time :)
    – kodlan
    Commented Jul 30, 2019 at 11:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.