Java Android可旋转图像视图

Java Android可旋转图像视图,java,android,graphics,canvas,Java,Android,Graphics,Canvas,我正在尝试创建一个可旋转的图像视图,我将指定该视图的特定角度和轴心点,并看到它围绕该轴心点旋转。我试过这样的方法: Matrix matrix = new Matrix(); matrix.postRotate(45, imageView.getWidth(), imageView.getHeight()); imageView.setScaleType(ScaleType.MATRIX); imageView.setImageMatrix(matrix); 但后旋转法的参数(第二和第三个轴点

我正在尝试创建一个可旋转的图像视图,我将指定该视图的特定角度和轴心点,并看到它围绕该轴心点旋转。我试过这样的方法:

Matrix matrix = new Matrix();
matrix.postRotate(45, imageView.getWidth(), imageView.getHeight());
imageView.setScaleType(ScaleType.MATRIX);
imageView.setImageMatrix(matrix);
但后旋转法的参数(第二和第三个轴点)没有任何变化。即使它们是0,0-这是一样的

所以我想创建一个ImageView,当初始化时它会旋转一定的角度。在本例中为45度。我试着设定界限和人员。。没有帮助


我该怎么做/

可以使用setRotation(int)旋转图像视图

参考:
编辑:我不得不缩短链接,因为url中有a),有些浏览器不喜欢这样。

这个功能对我来说很有用

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

这就是我在应用程序中使用view.setRotation(浮动角度)的方式,希望它能有所帮助:

//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);

根据

的回答,从左下角到右上角的图像是否比屏幕宽度或高度长?如果是这样的话,我不认为你可以旋转图像。不,它实际上是一个10x150维的图像。这是API版本>=11的图像-但我想这应该对我有用。无论如何,谢谢你,我会把它标记为答案。我在一个按钮中使用这个代码。它在第一次单击时工作正常,但在下一次单击时不再旋转。我是否应该添加其他行来修复此问题?@user2751628多次运行此代码不会有任何作用
setRotation
不会增加旋转,而是将其设置为45,您必须执行类似于
imageView.setRotation(imageView.getRotation())的操作
//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);