Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在旧矩阵中应用旋转变换_Android_Math_Matrix_Rotation_Imageview - Fatal编程技术网

Android 在旧矩阵中应用旋转变换

Android 在旧矩阵中应用旋转变换,android,math,matrix,rotation,imageview,Android,Math,Matrix,Rotation,Imageview,我有一个矩阵用于变换图像。旧矩阵是比例和平移的函数。 现在,如何在不影响其他变换(如缩放和平移)的情况下从旧矩阵的中心应用旋转 我已经试过了 //get old matrix Matrix matrix = getImageMatrix(); float tx = getMatrixValue(matrix, Matrix.MTRANS_X); float ty = getMatrixValue(matrix, Matrix.MTRANS_Y);

我有一个矩阵用于变换图像。旧矩阵是比例和平移的函数。 现在,如何在不影响其他变换(如缩放和平移)的情况下从旧矩阵的中心应用旋转

我已经试过了

    //get old matrix    
    Matrix matrix = getImageMatrix();

    float tx = getMatrixValue(matrix, Matrix.MTRANS_X);
    float ty = getMatrixValue(matrix, Matrix.MTRANS_Y);

    float scaleX = getMatrixValue(matrix, Matrix.MSCALE_X);
    float scaleY = getMatrixValue(matrix, Matrix.MSCALE_Y);

    float skewX = getMatrixValue(matrix, Matrix.MSKEW_X);
    float skewY = getMatrixValue(matrix, Matrix.MSKEW_Y);

    //calculating the actual scale
    float sx = (float)Math.sqrt((scaleX*scaleX)+(skewY*skewY));
    float sy = (float)Math.sqrt((scaleY*scaleY)+(skewX*skewX));

    //calculating the rotateAngle
    float rAngle = Math.round(Math.atan2(scaleX, skewX) * (180 / Math.PI));



    //calculate the actual width and height of image
    float width = sx * drawable.getIntrinsicWidth();
    float height = sy * drawable.getIntrinsicHeight();

    //calculate the center pivot for rotation
    float cx = (width/2)+tx;
    float cy = (height/2)+ty;

    //Applying Rotation from center pivot
    matrix.postTranslate(-cx , -cy);
    matrix.postRotate(rotateAngle, (width/2)+tx, (height/2)+ty);
    matrix.postTranslate(cx, cy);

    setImageMatrix(matrix);

    invalidate();
我没有得到想要的轮换结果。它使翻译发生了变化。我在这件事上做错了什么

查看完整代码(第220行至以后)
我不是100%确定,但我相信问题在于您试图提取tx和ty值,然后重新应用。矩阵有点“记住”已经做过的事情,所以如果您使用postRotate/postranslate/post*函数,就不需要这样做。请尝试以下方法:

Matrix matrix = getImageMatrix();
float rotateAngle = 90.0;  // or whatever

//calculate the actual width and height of image
float width = drawable.getIntrinsicWidth();
float height = drawable.getIntrinsicHeight();

//calculate the center pivot for rotation
float cx = (width/2);
float cy = (height/2);

//Applying Rotation from center pivot
matrix.postTranslate(-cx , -cy);
matrix.postRotate(rotateAngle);
matrix.postTranslate(cx, cy);
setImageMatrix(matrix);

invalidate();

我相信这会使你围绕中心旋转90度。

我不是100%确定,但我相信问题在于你试图提取tx和ty值,然后重新应用。矩阵有点“记住”已经做过的事情,所以如果您使用postRotate/postranslate/post*函数,就不需要这样做。请尝试以下方法:

Matrix matrix = getImageMatrix();
float rotateAngle = 90.0;  // or whatever

//calculate the actual width and height of image
float width = drawable.getIntrinsicWidth();
float height = drawable.getIntrinsicHeight();

//calculate the center pivot for rotation
float cx = (width/2);
float cy = (height/2);

//Applying Rotation from center pivot
matrix.postTranslate(-cx , -cy);
matrix.postRotate(rotateAngle);
matrix.postTranslate(cx, cy);
setImageMatrix(matrix);

invalidate();

我相信这会让你绕着中心旋转90度。

你就是这样做的。又短又甜

    private RectF rect = new RectF();   // only allocate once

    public void rotate(int rotateAngle) {

        if (rotateAngle % 90 != 0) {
            throw new IllegalArgumentException("angle must be a multiple of 90 degrees");
        }

        Matrix matrix = getImageMatrix();

        // get the original dimensions of the drawable
        rect.set(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

        // calculate where the current matrix has put the image
        matrix.mapRect(rect);  // rect now has updated coordinates

        // rotate pivoting on the center point
        matrix.postRotate(rotateAngle, rect.centerX(), rect.centerY());
        setImageMatrix(matrix); // i think setImageMatrix() will call invalidate() itself
    }

你就是这样做的。又短又甜

    private RectF rect = new RectF();   // only allocate once

    public void rotate(int rotateAngle) {

        if (rotateAngle % 90 != 0) {
            throw new IllegalArgumentException("angle must be a multiple of 90 degrees");
        }

        Matrix matrix = getImageMatrix();

        // get the original dimensions of the drawable
        rect.set(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

        // calculate where the current matrix has put the image
        matrix.mapRect(rect);  // rect now has updated coordinates

        // rotate pivoting on the center point
        matrix.postRotate(rotateAngle, rect.centerX(), rect.centerY());
        setImageMatrix(matrix); // i think setImageMatrix() will call invalidate() itself
    }

感谢您查找此。。!它无法从中心旋转。请检查我的完整代码,以便您能够理解我说的是。。!感谢您查找此。。!它无法从中心旋转。请检查我的完整代码,以便您能够理解我说的是。。!Thnx kris larson像天使一样出现解决我的问题:)。。!Thnx kris larson像天使一样出现解决我的问题:)。。!