开发者

Android Rotatable ImageView

开发者 https://www.devze.com 2023-04-05 09:39 出处:网络
I am trying to create a Rotatable 开发者_JS百科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:

I am trying to create a Rotatable 开发者_JS百科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? :/


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.


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


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;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号