Creating a 3D flip animation in Android using XML

后端 未结 8 1730
萌比男神i
萌比男神i 2020-12-22 20:25

I have created a 3D flip of a view using this android tutorial However, I have done it programmatically and I would like to do it all in xml, if possible. I am not talking

相关标签:
8条回答
  • 2020-12-22 21:02

    I have created a simple program for creating flip of view like :

    enter image description here

    In Activity you have to create this method, for adding flip_rotation in view.

    private void applyRotation(View view) 
    {
        final Flip3dAnimation rotation = new Flip3dAnimation(view);
        rotation.applyPropertiesInRotation();
        view.startAnimation(rotation);
    }
    

    for this, you have to copy main class used to provide flip_rotation.

    import android.graphics.Camera;
    import android.graphics.Matrix;
    import android.util.Log;
    import android.view.View;
    import android.view.animation.AccelerateInterpolator;
    import android.view.animation.Animation;
    import android.view.animation.Transformation;
    
    public class Flip3dAnimation extends Animation {
        private final float mFromDegrees;
        private final float mToDegrees;
        private final float mCenterX;
        private final float mCenterY;
        private Camera mCamera;
    
        public Flip3dAnimation(View view) {
            mFromDegrees = 0;
            mToDegrees = 720;
            mCenterX = view.getWidth() / 2.0f;
            mCenterY = view.getHeight() / 2.0f;
        }
    
        @Override
        public void initialize(int width, int height, int parentWidth,
                int parentHeight) {
            super.initialize(width, height, parentWidth, parentHeight);
            mCamera = new Camera();
        }
    
        public void applyPropertiesInRotation()
        {
            this.setDuration(2000);
            this.setFillAfter(true);
            this.setInterpolator(new AccelerateInterpolator());
        }
    
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            final float fromDegrees = mFromDegrees;
            float degrees = fromDegrees
                    + ((mToDegrees - fromDegrees) * interpolatedTime);
    
            final float centerX = mCenterX;
            final float centerY = mCenterY;
            final Camera camera = mCamera;
    
            final Matrix matrix = t.getMatrix();
    
            camera.save();
    
            Log.e("Degree",""+degrees) ;
            Log.e("centerX",""+centerX) ;
            Log.e("centerY",""+centerY) ;
    
            camera.rotateY(degrees);
    
            camera.getMatrix(matrix);
            camera.restore();
    
            matrix.preTranslate(-centerX, -centerY);
            matrix.postTranslate(centerX, centerY);
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-22 21:03

    Adding to A. Steenbergen's great answer. When flipping the same view (updating a TextView for example) I removed the View.Visibility change in the constructor in order to keep the transition smoother.

    public FlipListener(final View front, final View back) {
        this.mFrontView = front;
        this.mBackView = back;
    }
    
    0 讨论(0)
提交回复
热议问题