0

I have a one customview that extends ImageView in which i want to apply actions like Scale , Translate, Flip and Rotate using Matrix.

Scale and translate is working fine in ontouch events, and flip is also working fine in onclick. But the Rotate not as expected.

For Flip from the center

public void flip(String flipmode)
{
    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);

    Drawable drawable = getDrawable();
    if (drawable == null) {

        return ;
    }

    float width = scaleX * drawable.getIntrinsicWidth();
    float height = scaleY * drawable.getIntrinsicHeight();

    float skewX = getMatrixValue(matrix, Matrix.MSKEW_X);
    float rAngle = Math.round(Math.atan2(scaleX, skewX) * (180 / Math.PI));

    if (DEBUG) {

        Log.e(TAG, "onUp: " + tx + " " + ty);
        Log.e(TAG, "scaleX:    " + scaleX+"   scaleY:    " + scaleY);
        Log.e(TAG, "drawable size: " + drawable.getIntrinsicWidth() + " " + drawable.getIntrinsicHeight());
        Log.e(TAG, "Rotate Angle: " + rAngle);
        Log.e(TAG, "x center : " + (width/2)+tx + "   y center  " + (height/2)+ty);
    }

    if (flipmode == "ORIENTATION_FLIP_HORIZONTAL") {

        matrix.postScale(-1.0f, 1.0f, (width / 2) + tx, (height / 2) + ty);

    } else if (flipmode == "ORIENTATION_FLIP_VERTICAL") {


        matrix.postScale(1.0f, -1.0f, (width / 2) + tx, (height / 2) + ty);

    }


    setImageMatrix(matrix);
    invalidate();

}

For Rotate from the Center

public void rotate(int rotateAngle)
{
    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);


    Drawable drawable = getDrawable();
    if (drawable == null) {

        return ;
    }

    float sx = (float)Math.sqrt((scaleX*scaleX)+(skewY*skewY));
    float sy = (float)Math.sqrt((scaleY*scaleY)+(skewX*skewX));

    float width = sx * drawable.getIntrinsicWidth();
    float height = sy * drawable.getIntrinsicHeight();

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

    if (DEBUG) {

        Log.e(TAG, "onUp: " + tx + " " + ty);
        Log.e(TAG, "scaleX:    " + scaleX+"   scaleY:    " + scaleY);
        Log.e(TAG, "scaleX new :    " + sx+"   scaleY new:    " + sy);
        Log.e(TAG, "drawable size: " + drawable.getIntrinsicWidth() + " " + drawable.getIntrinsicHeight());
        Log.e(TAG, "Rotate Angle: " + rAngle);
        Log.e(TAG, "x center : " + (width/2)+tx + "   y center  " + (height/2)+ty);
    }



    matrix.postRotate(rotateAngle, (width/2)+tx, (height/2)+ty);

    setImageMatrix(matrix);

    invalidate();

}

i have seen very strange behavior on after first rotation. issue : new coordinates are not as expected Log Initial :

ImageView: onUp: 144.0 0.0
ImageView: scaleX:    0.6   scaleY:    0.6
ImageView: scaleX new :    0.6   scaleY new:    0.6
ImageView: drawable size: 800 1280
ImageView: Rotate Angle: 90.0
ImageView: x center : 240.00002144.0   y center  384.00.0

Log : after applying rotation

ImageView: onUp: 0.0 624.0
ImageView: scaleX:    0.0   scaleY:    0.0
ImageView: scaleX new :    0.6   scaleY new:    0.6
ImageView: drawable size: 800 1280
ImageView: Rotate Angle: 0.0
ImageView: x center : 240.000020.0   y center  384.0624.0

How to apply rotation in old matrix properly..?

Hardik Kubavat
  • 251
  • 3
  • 23

0 Answers0