I read the source code of a small game on Github, and found that the animation effects of a Camera class are quite good. I hereby mark it. Note that there are two Camera classes in the Android Api. One is under the Hardware package, which we are familiar with when calling cameras, and the other is under the Graphics package, which is the hero of the day. Here’s a GIF to see what it looks like:





Main code:

public class Camera3dAnimation extends Animation {
    private Camera camera;
    //3D rotate;
    private float centerX;
    private float centerY;
    private View fromView;
    private View toView;
    private boolean forward = true; public Camera3dAnimation(View fromView, View toView) { this.fromView = fromView; this.toView = toView; // The duration of the animationsetDuration(1000);
        setFillAfter(false);
        setInterpolator(new AccelerateDecelerateInterpolator()); } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); // centerX = width / 2; centerY = height / 2; camera = new Camera(); } @Override protected void applyTransformation(floatinterpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); //interpolated the value is 0.0 to 1.0float degrees = (float) (180.0 * interpolatedTime); // Display toView after halfifInterpolatedTime >= 0.5f) {degrees -= 180. F; fromView.setVisibility(View.GONE); toView.setVisibility(View.VISIBLE); } // Rotate 180 degrees around the Y axis (counterclockwise) and the next animation will be clockwiseif(forward) degrees = -degrees; final Matrix matrix = t.getMatrix(); camera.save(); // Rotate camera. RotateY (degrees); camera.getMatrix(matrix); camera.restore(); PreTranslate (-centerx, -centery); //preTranslate moves before rotation while postTranslate moves after rotation. matrix.postTranslate(centerX, centerY); } /** * When an animation is completed, the next animation swaps heads and tails */ public voidreverse() {
        forward = false; View switchView = toView; toView = fromView; fromView = switchView; }}Copy the code

Here we see the realization of the Animation effect is still to use the Animation class, mainly overwrite the two methods of the Aniamtion class. Initialized Here you can initialize related parameters such as setting the duration of an animation, Interpolator, and reference point of an animation. The other method, applyTransformation, determines the animation effect. This method is called repeatedly during the animation process. The general idea is to obtain the Matrix object by Transformation, and to achieve the animation effect by changing the Matrix. Camera. RotateY (degrees) indicates the number of degrees rotated around the Y-axis, and the degrees parameter changes over time during the animation. PreTranslate moves before the rotation while postTranslate moves after the rotation is complete. Its main function is to rotate the object around its center.

The source code