Android Rotatable ImageView

风流意气都作罢 提交于 2019-12-18 05:01:17

问题


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? :/


回答1:


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.




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/7437732/android-rotatable-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!