我也爲此奮鬥了一段時間。通過指定一個4x4變換矩陣併爲每一幀計算它會很容易,但不幸的是,Android中只有一個3x3矩陣對象,不支持3D旋轉和平移。我的解決方案翻轉兩個對角線:
public class RightDiagonalFlipAnimation extends Animation {
private float fromDegree;
private float toDegree;
private float densityDpi;
private float width;
private Camera camera;
public RightDiagonalFlipAnimation(float fromDegree, float toDegree, float densityDpi) {
this.fromDegree = fromDegree;
this.toDegree = toDegree;
this.densityDpi = densityDpi;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
this.width = width;
camera = new Camera();
float cameraDistance = width * 25;
camera.setLocation(0, 0, -cameraDistance/densityDpi);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float xDegrees = fromDegree + ((toDegree - fromDegree) * interpolatedTime);
final Matrix matrix = t.getMatrix();
camera.save();
camera.rotateX(-xDegrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-this.width/(float) Math.sqrt(2), -this.width/(float) Math.sqrt(2));
matrix.postTranslate(this.width/(float)Math.sqrt(2), this.width/(float)Math.sqrt(2));
matrix.postRotate(-45);
matrix.preRotate(45);
}
}
public class LeftDiagonalFlipAnimation extends Animation {
private float fromDegree;
private float toDegree;
private float densityDpi;
private float width;
private Camera camera;
public LeftDiagonalFlipAnimation(float fromDegree, float toDegree, float densityDpi) {
this.fromDegree = fromDegree;
this.toDegree = toDegree;
this.densityDpi = densityDpi;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
this.width = width;
camera = new Camera();
float cameraDistance = width * 25;
camera.setLocation(0, 0, -cameraDistance/densityDpi);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float xDegrees = fromDegree + ((toDegree - fromDegree) * interpolatedTime);
final Matrix matrix = t.getMatrix();
camera.save();
camera.rotateX(xDegrees);
camera.getMatrix(matrix);
camera.restore();
matrix.postRotate(45);
matrix.preRotate(-45);
}
}
來源
2016-05-19 19:43:59
Ish
最新錯了?我試過你的代碼,它的工作 – Raghunandan
它是一個簡單的翻轉,但我需要一個對角翻轉... –