This article mainly describes the Android stereo rotation animation, or 3D rotation, is my own implementation of an interface
Three-dimensional rotation is divided into the following three types:
1. Rotate the axis with the x axis
2. Rotate the axis on the y axis
3. Rotate Z Axis -this is equivalent to the Android default self-contained rotation animation rotateanimation
To achieve the three-dimensional rotation core steps:
1. Inheriting the system Animation overriding the applytransformation method
Use the callback parameter of the applytransformation method float interpolatedtime, transformation T to control the rotation animation
interpolatedtime is used to calculate the rotation angle and t is used to control the transformation matrix to achieve the rotation of the image
2. android.graphics.Camera Control rotation algorithm
Camera can perform some more complex operations on the image-rotate, bloom, and tilt the image with the Matrix
Core code encapsulation:rotate3danimation
PackageRotateanim.example.com.androidrotateanim;Importandroid.view.animation.Animation;Importandroid.view.animation.Transformation;ImportAndroid.graphics.Camera;ImportAndroid.graphics.Matrix;/*** A animation that rotates the view on the x, y, Z axis between and specified angles. * This animation also adds a TR Anslation on the Z axis (depth) to improve the effect. */ Public classRotate3danimationextendsAnimation { Public Static FinalByte Rotate_x_axis = 0x00; Public Static FinalByte Rotate_y_axis = 0x01; Public Static FinalByte Rotate_z_axis = 0x02; Private Final floatmfromdegrees; Private Final floatmtodegrees; Private Final floatMcenterx; Private Final floatMcentery; Private Final floatMdepthz; Private Final BooleanMreverse; PrivateCamera Mcamera; private Byte Mrotateaxis; 0:x Axis 1:y Axis 2:z Shaft /**Create a 3D rotation animation *@paramfromdegrees The start angle of the 3D rotation *@paramtodegrees the end angle of the 3D rotation *@paramCenterX The X center of the 3D rotation *@paramCenterY The Y center of the 3D rotation *@paramDepthz The Z depth of the 3D rotation *@paramRotateAxis The rotate axis of the 3D rotation *@paramReverse True if the translation should be reversed, false otherwise*/ PublicRotate3danimation (floatFromdegrees,floatTodegrees,floatCenterX,floatCenterY,floatDepthz, Byte RotateAxis,Booleanreverse) {Mfromdegrees=fromdegrees; Mtodegrees=todegrees; Mcenterx=CenterX; Mcentery=CenterY; Mdepthz=Depthz; Mrotateaxis=RotateAxis; Mreverse=reverse; } @Override Public voidInitializeintWidthintHeightintParentwidth,intparentheight) { Super. Initialize (width, height, parentwidth, parentheight); Mcamera=NewCamera (); } @Overrideprotected voidApplytransformation (float interpolatedtime, transformation t) { Final floatFromdegrees =mfromdegrees; floatdegrees = Fromdegrees + ((mtodegrees-fromdegrees) * interpolatedtime); Final floatCenterX =Mcenterx; Final floatCenterY =Mcentery; Final Camera camera = Mcamera; final Matrix matrix = T.getmatrix (); // Save the current camera position so that the transformation is completed and restored to its original location Camera.save (); if(mreverse) {//The offset of Z will be more and more large. This creates an effect that looks from near to far Camera.translatE (0.0f, 0.0f, Mdepthz *interpolatedtime); } Else { //The offset of z will be less and less. This will result in an effect where our view is moving from one place to the next, nearer and farther, and finally moving to our window.Camera.translate (0.0f, 0.0f, Mdepthz * (1.0f-interpolatedtime)); } //is to add a rotation effect to our view, and during the move, the view will also rotate at the center of the XYZ axis. 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 the series of transformations we have just defined to the transformation matrix, and after the call, we can restore the camera's location for the next use. Camera.getmatrix (matrix); //Camera Location Recovery Camera.restore (); //The following two sentences are for the animation to be in the view Center for the rotation point matrix.pretranslate (-centerx,- centery); Matrix.posttranslate (CenterX, centery); }}
Rotate3danimation use: With normal animation use no difference, set to a View object, start the animation is done
MROTATEIMGV is the view object that needs to be rotated //rotates on the x axisPrivate void rotateonxcoordinate() {floatCenterX = Mrotateimgv.getwidth ()/2.0f; floatCenterY = Mrotateimgv.getheight ()/2.0f; floatDepthz =0f; Rotate3danimation Rotate3danimationx=NewRotate3danimation (0, CenterX, CenterY, Depthz, Rotate3danimation.rotate_x_axis,true); Rotate3danimationx.setduration (1000); MROTATEIMGV. startanimation (Rotate3danimationx);}//rotate by axis on X axisPrivate void rotateonycoordinate() {floatCenterX = Mrotateimgv.getwidth ()/2.0f; floatCenterY = Mrotateimgv.getheight ()/2.0f; floatCenterz =0f; Rotate3danimation Rotate3danimationx=NewRotate3danimation (0, CenterX, CenterY, Centerz, Rotate3danimation.rotate_y_axis,true); Rotate3danimationx.setduration (1000); Mrotateimgv.startanimation (Rotate3danimationx);}//Z axis rotation---equivalent to the normal plane rotation animationPrivate void Rotateanimhorizon() {floatCenterX = Mrotateimgv.getwidth ()/2.0f; floatCenterY = Mrotateimgv.getheight ()/2.0f; floatCenterz =0f; Rotate3danimation Rotate3danimationx=NewRotate3danimation (0, CenterX, CenterY, Centerz, Rotate3danimation.rotate_z_axis,true); Rotate3danimationx.setduration (1000); Mrotateimgv.startanimation (Rotate3danimationx); //Here's a spin animation with Android//rotateanimation rotateanimation = new Rotateanimation (0, Animation.relative_to_self, 0.5f, animation.relative _to_self, 0.5f); //rotateanimation.setduration (1000); //mrotateimgv.startanimation (rotateanimation);}
Android Stereo rotation animation implementation and encapsulation (support for axis rotation on X, Y, z three axes)