7

I am trying to create a Rotatable an ImageView to which I will specify certain angle and pivot point and see it rotated around that pivot point. I tried something like this:

Matrix matrix = new Matrix();
matrix.postRotate(45, imageView.getWidth(), imageView.getHeight());
imageView.setScaleType(ScaleType.MATRIX);
imageView.setImageMatrix(matrix);

but the parameters of postRotate method (the second and third - the pivot points) make NO CHANGE at all. even if they are 0, 0 - it's the same thing.

So I wanna create a ImageView that would be rotated by certain angle when initialized. In this example 45 degrees. I tried setting the bounds and staff.. no help.

How do I do that? :/

Tzanter
  • 73
  • 1
  • 1
  • 5
  • Is the image from bottom left corner to top right corner longer than either the screen width or height? I don't think you can rotate the image if that's the case. – ahodder Sep 15 '11 at 21:43
  • no, it's actually an image of 10x150 dimension. – Tzanter Sep 15 '11 at 21:56

3 Answers3

17

You can rotate a ImageView by using setRotation(int);

// rotate imageView 45 around center pivot point
imageView.setPivotX(imageView.getWidth()/2);
imageView.setPivotY(imageView.getHeight()/2);
imageView.setRotation(45);

Reference: http://goo.gl/WhhGM Edit: I had to shorten the link because of a ) in the url, some browsers don't like that.

Spencer
  • 1,476
  • 1
  • 14
  • 26
  • that's for API version >= 11 - but that should work for me I suppose. Thanks anyway, will mark it as answer. – Tzanter Sep 15 '11 at 22:12
  • I'm using this code in a button. It works perfectly in the first click, but doesn't rotate anymore on next clicks. Should I add some other line to fix this? – Eggakin Baconwalker Aug 25 '16 at 20:09
  • 1
    @user2751628 running this code multiple times won't do anything. `setRotation` doesn't increase the rotation, it sets it to 45, you will have to do something like `imageView.setRotation(imageView.getRotation());` – Spencer Aug 26 '16 at 01:37
0

This is how I use view.setRotation(float angle) in my apps, hope it will be helpful:

//to make rotation use next code
imageView.setPivotX(imageView.getWidth() / 2);
imageView.setPivotY(imageView.getHeight() / 2);
imageView.setRotation(45);

//to reset rotate state to initial position    
imageView.setPivotX(imageView.getWidth() / 2);
imageView.setPivotY(imageView.getHeight() / 2);    
imageView.setRotation(0);

Based on answer from Spencer

ardiien
  • 767
  • 6
  • 26
-1

This function works for me.

public static Bitmap rotateImage (Bitmap srcBitmap, int width, int height, int rotation)
    {
        // create rotated image
        Matrix matrix = new Matrix();
        rotation =  (rotation +1 )   % 3;
        rotation = rotation * 90;
        matrix.postRotate( rotation,
                width,
                height );
        Bitmap rotatedBmp = Bitmap.createBitmap( srcBitmap,
                0,
                0,
                srcBitmap.getWidth(),
                srcBitmap.getHeight(),
                matrix,
                false );

        return rotatedBmp;
    }
M.Hefny
  • 2,677
  • 1
  • 26
  • 30