There are a lot of nice examples in the Android API demos, and the code for these examples is pretty good, so if you're working through every single example in the API demos, congratulations on being a real Android player. It's also a way for some of the more confused Android developers to point out an ability to improve themselves. There are many examples in API demos, today we will imitate one of the 3D transformation of the special effects, to achieve a different image browser.
Since it is a special effect to do the rotation of the axis, it is definitely necessary to use the function of 3D transformation. In Android, if you want to achieve 3D effect there are generally two options, one is to use open GL ES, and the other is to use camera. Open GL es are too complex to use, typically used for more advanced 3D effects or games, such as simple 3D effects, using camera is sufficient.
Three methods of rotation are provided in the camera. The Rotatex (), Rotatey (), and Rotatez, which call these three methods and pass in the appropriate angle, allow the view to rotate around the three axes, and today we're going to do the central axis rotation to make the view revolve around the y axis. Rotating. Sketch diagram using camera to rotate the view, as follows:
So let's start by creating an Android project named Rotatepicbrowserdemo, and then we've got a couple of pictures for later browsing in the picture browser.
The API demos has provided us with a very useful 3D rotary animation tool class Rotate3danimation, this tool class is implemented using camera, we first copy this class to the project, the code is as follows:
/** * A animation that rotates's view on the Y axis between two specified.
* This animation also adds a translation on the Z axis (depth) to improve the effect.
* * public class Rotate3danimation extends Animation {private final float mfromdegrees;
Private final float mtodegrees;
Private final float Mcenterx;
Private final float mcentery;
Private final float Mdepthz;
Private Final Boolean mreverse;
Private Camera Mcamera; /** * Creates a new 3D rotation on the Y axis. The rotation is defined through its * start angle and its-end angle. Both angles are in degrees. The rotation * is performed around "a center point" on the 2D spaces, definied by a pair * of X and Y coordinates , called CenterX and CenterY. When the animation * starts, a translation on the Z axis (depth) is performed. The length * of the translation can be specified, as the as-as-whether the translation * should be reverSed in time.
* * @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 * @par AM reverse true if the translation should be reversed, false otherwise/public rotate3danimation (float fromdeg Rees, float todegrees, float centerx, float centery, float depthz, Boolean reverse) {Mfromdegrees
= Fromdegrees;
Mtodegrees = todegrees;
Mcenterx = CenterX;
Mcentery = CenterY;
Mdepthz = Depthz;
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 (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 ();
if (mreverse) {camera.translate (0.0f, 0.0f, Mdepthz * interpolatedtime);
else {camera.translate (0.0f, 0.0f, Mdepthz * (1.0f-interpolatedtime));
} camera.rotatey (degrees);
Camera.getmatrix (matrix);
Camera.restore ();
Matrix.pretranslate (-centerx,-centery);
Matrix.posttranslate (CenterX, centery); }
}