java Android 可旋转 ImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7437732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Rotatable ImageView
提问by Tzanter
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:
我正在尝试创建一个可旋转的 ImageView,我将指定特定角度和枢轴点并查看它围绕该枢轴点旋转。我试过这样的事情:
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.
但是 postRotate 方法的参数(第二个和第三个 - 枢轴点)根本没有改变。即使它们是 0, 0 - 也是一回事。
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.
所以我想创建一个 ImageView,它在初始化时会旋转一定的角度。在本例中为 45 度。我试着设置界限和工作人员......没有帮助。
How do I do that? :/
我怎么做?:/
回答by Spencer
You can rotate a ImageView by using setRotation(int);
您可以使用 setRotation(int) 来旋转 ImageView;
// rotate imageView 45 around center pivot point
imageView.setPivotX(imageView.getWidth()/2);
imageView.setPivotY(imageView.getHeight()/2);
imageView.setRotation(45);
Reference: http://goo.gl/WhhGMEdit: I had to shorten the link because of a ) in the url, some browsers don't like that.
参考:http: //goo.gl/WhhGM编辑:由于 url 中的 ),我不得不缩短链接,有些浏览器不喜欢那样。
回答by Yaroslav Ovdiienko
This is how I use view.setRotation(float angle) in my apps, hope it will be helpful:
这就是我在我的应用程序中使用 view.setRotation(float angle) 的方式,希望它会有所帮助:
//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
根据斯宾塞的回答
回答by M.Hefny
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;
}