Android stereo rotation animation implementation and encapsulation (supporting axis rotation with three axes X, Y, and Z), android stereo Rotation

Source: Internet
Author: User

Android stereo rotation animation implementation and encapsulation (supporting axis rotation with three axes X, Y, and Z), android stereo Rotation

This article mainly introduces Android stereo rotation animation or 3D Rotation, which is a self-implemented interface.

 

Three-dimensional rotation is divided into the following three types:

1.X axisRotate for Axis

2.Y axisRotate for Axis

3.Z axisFor axis rotation-this is equivalent to the default android built-in rotation AnimationRotateAnimation

 

Core steps for three-dimensional rotation:

  1.Inheritance SystemAnimationRewriteApplyTransformationMethod

PassApplyTransformationMethod callback ParametersFloat interpolatedTime, Transformation tTo control rotation Animation

    InterpolatedTimeUsed to calculate the Rotation Angle andTUsed to control the transformation matrix for image rotation

2. android. graphics. CameraControl Rotation Algorithm

    CameraYou can perform some complex operations on the image-rotating, blooming, andMatrixImage skew

 

Core code encapsulation:Rotate3dAnimation

Package rotateanim.example.com. androidrotateanim; import android. view. animation. animation; import android. view. animation. transformation; import android. graphics. camera; import android. graphics. matrix;/*** An animation that rotates the view on the X, Y, Z axis between two specified angles. * This animation also adds a translation on the Z axis (depth) to improve the effect. */public class Rotate3dAnimation extends Animation {public static final ByteROTATE_X_AXIS= 0x00; public static final ByteROTATE_Y_AXIS= 0x01; public static final ByteROTATE_Z_AXIS= 0x02; private final float indexes; private final float mToDegrees; private final float mCenterX; private final float mCenterY; private final float mDepthZ; private final boolean mReverse; private Camera mCamera;Private Byte mRotateAxis; // 0: X axis 1: Y axis 2: Z axis/** Create a 3D rotation animation * @ param fromDegrees the start angle of the 3D rotation * @ param toDegrees the end angle of the 3D rotation * @ param centerX the X center of the 3D rotation * @ param centerY the Y center of the 3D rotation * @ param depthZ the Z depth of the 3D rotation * @ param rotateAxis the rotate axis of the 3D rotation * @ param reverse true if translation shoshould be reversed, false otherwise */public values (float fromDegrees, float toDegrees, float centerX, float centerY, float depthZ, Byte rotateAxis, boolean reverse) {values = fromDegrees; values = toDegrees; mCenterX = centerX; mCenterY = centerY; mDepthZ = depthZ; region = rotateAxis; mReverse = reverse;} @ Override public void initialize (int width, int height, int parentWidth, int parentHeight) {super. initialize (width, height, parentWidth, parentHeight); mCamera = new Camera () ;}@ Override protected void applyTransformation (floatInterpolatedTime, TransformationT) {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 ();// Save the current camera position so that the camera can be restored to the original position after the conversion is completed.Camera. save ();The offset of if (mReverse) {// z will increase. This will form such an effect, the view from near to farCamera. translatE (0.0f, 0.0f, mDepthZ * interpolatedTime);} the offset of else {// z will be smaller and smaller. This will form this effect. Our View is moving from a long distance to us, getting closer and closer, and finally moved to camera above our window. translate (0.0f, 0.0f, mDepthZ * (1.0f-interpolatedTime ));}// Add the rotation effect to our View. During the moving process, the View will be rotated with the XYZ axis as the center.If (ROTATE_X_AXIS.equals (mRotateAxis) {camera.RotateX(Degrees);} else if (ROTATE_Y_AXIS.equals (mRotateAxis) {camera.RotateY(Degrees);} else {camera.RotateZ(Degrees );}// This is to apply a series of transformations we have just defined to the transformation matrix. After this sentence is called, we can restore the location of camera for the next use. Camera. getMatrix (matrix );// Restore camera. restore ();// The following two sentences are used to show that the animation is centered on the View.Matrix. preTranslate (-centerX ,-CenterY); matrix. postTranslate (centerX, centerY );}}

 

Rotate3dAnimation: it is no different from normal animation. Set it to a View object and start the animation.

MRotateImgv is the View object to be rotated.// Rotate private void with the X axisRotateOnXCoordinate() {Float centerX = mRotateImgv. getWidth ()/2.0f; float centerY = mRotateImgv. getHeight ()/2.0f; float depthZ = 0f; Rotate3dAnimation rotate3dAnimationX = new Rotate3dAnimation (0,180, centerX, centerY, depthZ, Rotate3dAnimation. ROTATE_X_AXIS, true); rotate3dAnimationX. setDuration (1000); mRotateImgv.StartAnimation(Rotate3dAnimationX );}// Rotate with the X axisPrivate voidRotateOnYCoordinate() {Float centerX = mRotateImgv. getWidth ()/2.0f; float centerY = mRotateImgv. getHeight ()/2.0f; float centerZ = 0f; Rotate3dAnimation rotate3dAnimationX = new Rotate3dAnimation (0,180, centerX, centerY, centerZ, Rotate3dAnimation. ROTATE_Y_AXIS, true); rotate3dAnimationX. setDuration (1000); mRotateImgv. startAnimation (rotate3dAnimationX );}// Take the Z axis as the axis for rotation-equivalent to a normal plane rotation AnimationPrivate voidRotateAnimHorizon() {Float centerX = mRotateImgv. getWidth ()/2.0f; float centerY = mRotateImgv. getHeight ()/2.0f; float centerZ = 0f; Rotate3dAnimation rotate3dAnimationX = new Rotate3dAnimation (180, 0, centerX, centerY, centerZ, Rotate3dAnimation. ROTATE_Z_AXIS, true); rotate3dAnimationX. setDuration (1000); mRotateImgv. startAnimation (rotate3dAnimationX); // The following is a rotation Animation that comes with android // RotateAnimation rotateAnimation = new RotateAnimation (0,180, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f); // rotateAnimation. setDuration (1000); // mRotateImgv. startAnimation (rotateAnimation );}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.