Android——3D旋转变换的动画封装

    xiaoxiao2021-03-25  168

    实现3D旋转动画的封装

    public class FlipAnimator extends Animation { public static final int DIRECTION_X = 1, DIRECTION_Y = 2, DIRECTION_Z = 3; private Camera camera; private View fromView; private View toView; private final float centerX; private final float centerY; private boolean forward = true; private boolean visibilitySwapped; private int rotationDirection = DIRECTION_X; private int translateDirection = DIRECTION_Z; /** * Creates a 3D flip animation between two views. If forward is true, its * assumed that view1 is "visible" and view2 is "gone" before the animation * starts. At the end of the animation, view1 will be "gone" and view2 will * be "visible". If forward is false, the reverse is assumed. * * @param fromView First view in the transition. * @param toView Second view in the transition. * @param centerX The center of the views in the x-axis. * @param centerY The center of the views in the y-axis. */ public FlipAnimator(View fromView, View toView, int centerX, int centerY) { this.fromView = fromView; this.toView = toView; this.centerX = centerX; this.centerY = centerY; setDuration(500); //是因为有动画链的原因,假定你有一个移动的动画紧跟一个淡出的动画,如果你不把移动的动画的setFillAfter置为true, // 那么移动动画结束后,View会回到原来的位置淡出,如果setFillAfter置为true, 就会在移动动画结束的位置淡出 setFillAfter(true); setInterpolator(new AccelerateDecelerateInterpolator()); } public int getRotationDirection() { return rotationDirection; } public void setRotationDirection(int rotationDirection) { this.rotationDirection = rotationDirection; } public int getTranslateDirection() { return translateDirection; } public void setTranslateDirection(int translateDirection) { this.translateDirection = translateDirection; } public void reverse() { forward = false; View temp = toView; toView = fromView; fromView = temp; } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); camera = new Camera(); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { // Angle around the y-axis of the rotation at the given time. It is // calculated both in radians and in the equivalent degrees. final double radians = Math.PI * interpolatedTime; float degrees = (float) (180.0 * radians / Math.PI); // Once we reach the midpoint in the animation, we need to hide the // source view and show the destination view. We also need to change // the angle by 180 degrees so that the destination does not come in // flipped around. This is the main problem with SDK sample, it does not // do this. if (interpolatedTime >= 0.5f) { degrees -= 180.f; if (!visibilitySwapped) { fromView.setVisibility(View.GONE); toView.setVisibility(View.VISIBLE); visibilitySwapped = true; } } if (forward) degrees = -degrees; //获取变换的矩阵 final Matrix matrix = t.getMatrix(); camera.save(); //策略模式,根据不同的策略设置相机的变换方位 if(translateDirection == DIRECTION_Z) { camera.translate(0.0f, 0.0f, (float) (150.0 * Math.sin(radians))); } else if(translateDirection == DIRECTION_Y) { camera.translate(0.0f, (float) (150.0 * Math.sin(radians)), 0.0f); } else { camera.translate( (float) (150.0 * Math.sin(radians)), 0.0f, 0.0f); } if(rotationDirection == DIRECTION_Z) { camera.rotateZ(degrees); } else if(rotationDirection == DIRECTION_Y) { camera.rotateY(degrees); } else { camera.rotateX(degrees); } camera.getMatrix(matrix); camera.restore(); //将视图移动到坐标中心,进行旋转 matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } }
    转载请注明原文地址: https://ju.6miu.com/read-2538.html

    最新回复(0)