Wednesday, 3 November 2021

Why postRotate() method change X axis direction

I'm trying to reflect a drawn sticker like this

Original Sticker ---------- Reflected Sticker

      ↓                           ↓
public void reflectCurrentSticker(int windowWidth) {

    //     Y
    // ----|----- x
    //     |

    //creation of the cloned sticker
    //getWidth() == width of the FrameLayout (where the stickers drawn)

    Matrix originalMatrix = getReflectedMatrix(getWidth(), originalSticker);
    addSticker(clonedSticker);
    clonedSticker.setMatrix(originalMatrix);
    invalidate();

}

public Matrix getReflectedMatrix(int wrapperWidth, Sticker sticker) {
    Matrix matrix = sticker.getMatrix();
    float transX = getMatrixValue(matrix, 2);
    float transY = getMatrixValue(matrix, 5);
    float newX = (((float) wrapperWidth) - transX) - ((float) sticker.getCurrentWidth());
    float currentAngle = sticker.getCurrentAngle();
    float currentScale = sticker.getCurrentScale();
    Matrix newMatrix = new Matrix();
    newMatrix.postRotate(currentAngle);
    newMatrix.postScale(currentScale, currentScale);
    newMatrix.postTranslate(newX, transY);
    return newMatrix;
}

public float getMatrixValue(@NonNull Matrix matrix, @IntRange(from = 0, to = 9) int valueIndex) {
        final float[] matrixValues = new float[9];
        matrix.getValues(matrixValues);
        return matrixValues[valueIndex];
}

My code works fine, but the issue starts when I rotate the original sticker, and try to create a reflected sticker from it, unfortunately, I got this, the reflected sticker positioned in the wrong place, with the wrong rotation angle.

Original Sticker -- Reflected Sticker

      ↓                 ↓

The expected output is :

Original Sticker ---------- Reflected Sticker

      ↓                           ↓


from Why postRotate() method change X axis direction

No comments:

Post a Comment