我在试着把这样的画画出来
原始贴纸
↓ ↓
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];
}我的代码很好,但是当我旋转原来的贴纸时,问题就开始了,然后尝试从它创建一个反射贴纸,不幸的是,我得到了这个,反射贴纸定位在错误的地方,旋转角度不对。
原始贴纸 -- 反射贴纸
↓ ↓
预期产出如下:
原始贴纸
↓ ↓
发布于 2021-10-26 05:21:03
为什么postRotate()方法改变X轴方向?
可能看起来是这样,但事实并非如此。首先,您需要理解translate()、rotate()和screw()操作需要一个支点。枢轴点位于任何图像/位图的最左上方。其次,Android总是顺时针旋转图像/位图,而不是逆时针旋转。如果你想逆时针旋转它,你可以否定e.i的角度。90到-90,但不推荐它--它可能会导致错误。而是360 - angle - 180。这大概就是你想要的。
newMatrix.postScale(-currentScale, currentScale);
newMatrix.postTranslate(sticker.getCurrentWidth, transY);
newMatrix.postRotate(360 - currentAngle - 180);https://stackoverflow.com/questions/69715786
复制相似问题