我试图反射(reflect)这样的绘制贴纸
原贴 ---------- 反光贴
↓ ↓
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];
}
我的代码工作正常,但是当我旋转原始贴纸并尝试从中创建反射贴纸时,问题就开始了,不幸的是,我得到了这个,反射贴纸放置在错误的位置,旋转角度错误。原贴 -- 反光贴
↓ ↓
预期的输出是:
原贴 ---------- 反光贴
↓ ↓
最佳答案
Why postRotate() method change X axis direction?
它可能看起来是,它不是。首先,您需要了解
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);
关于java - 为什么 postRotate() 方法改变 X 轴方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69715786/