Original address: http://blog.csdn.net/x_i_a_o_h_a_i/article/details/40449847
In fact, there are many examples of 3D rotation on the net, here I just want to make an explanation of their code.
First:
Code:
Turnaroundactivity
/** * Main activity of the image browser. * * @author Guolin */public class Turnaroundactivity extends Activity {/** * root layout */private relativelayout layout;/** * for Show Picture List of ListView */private ListView piclistview;/** * Flipped layout */private linearlayout picture;/** * Picture List adapter */private Pictur Eadapter adapter;/** * holds a collection of all pictures */private list<picture> piclist = new arraylist<picture> (); @ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (r.layout.activity_around);// Initialize the picture list data initpics (); layout = (relativelayout) Findviewbyid (r.id.layout);p Iclistview = (ListView) Findviewbyid ( R.id.pic_list_view);p icture = (linearlayout) Findviewbyid (r.id.picture); adapter = new Pictureadapter (this, 0, piclist) ;p Iclistview.setadapter (Adapter);p Iclistview.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> parent, view view, int position, long ID) {//Gets the center point position of the layout as a rotationThe center point of float CenterX = Layout.getwidth ()/2f;float centery = Layout.getheight ()/2f;//constructs a 3D rotation animation object with a rotation angle of 0-90 degrees, which allows the ListView to From visible to invisible final rotate3danimation rotation = new Rotate3danimation (0, CenterX, centery,310.0f, true);// Animation duration 500 ms Rotation.setduration (500);//The State Rotation.setfillafter (true) to remain complete after the animation is complete; Rotation.setinterpolator (new Accelerateinterpolator ());//Set the listener Rotation.setanimationlistener of the animation (new Turntoimageview ()); Layout.startanimation ( rotation);}); Picture.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//Gets the center point position of the layout as the center point of the rotation float CenterX = Layout.getwidth ()/2f;float centery = Layout.getheight ()/2f;//building 3D rotating animated objects with a rotation angle of 360-270 degrees This causes the ImageView to become invisible from the visible, and the direction of rotation is the opposite of final rotate3danimation rotation = new Rotate3danimation (All-in-one, +, Centerx,centery, 310.0f, True);//Animation duration 500 ms Rotation.setduration (500);//The state of completion after the animation is complete rotation.setfillafter (true); Rotation.setinterpolator (New Accelerateinterpolator ());//Set the listener for the animation Rotation.setanimationlistener (new TurnToListView ()); layout.startanimation (rotation);}}); /** * Initialize Picture list data. */private void Initpics () {Picture bird = new Picture ("Bird", R.drawable.bird);p iclist.add (bird); Picture winter = new picture ("Winter", R.drawable.winter);p iclist.add (winter); Picture autumn = new picture ("Autumn", R.drawable.autumn);p Iclist.add (Autumn); Picture GreatWall = new Picture ("Great Wall", R.drawable.great_wall);p Iclist.add (GreatWall); Picture WaterFall = new Picture ("Water Fall", R.drawable.water_fall);p Iclist.add (WaterFall);} /** * Register the animation listener in the ListView click Animation to complete the subsequent animation of the ListView. * * @author Guolin */class Turntoimageview implements Animationlistener {@Overridepublic void Onanimationstart (Animation Animation) {}/** * When the ListView animation is complete, you also need to start the ImageView animation again, so that the ImageView is invisible to the visible */@Overridepublic void Onanimationend ( Animation Animation) {//Get the center point position of the layout as the center point of the rotation float CenterX = layout.getwidth ()/2f;float centery = layout.getheight ()/2f ;//Will ListView hidden Piclistview.setvisibility (View.gone);//Will ImageView display picture.setvisibility (View. Visible);p icture.requestfocus ();//build 3D Rotate animation object with a rotation angle of 270-360 degrees, which makes ImageView visible to final rotate3danimation rotation = new Rotate3danimation (+, CenterX, centery,310.0f, false);//Animation duration 500 ms Rotation.setduration (500);// The state Rotation.setfillafter (true) to remain complete after the animation is complete; Rotation.setinterpolator (new Accelerateinterpolator ()); Layout.startanimation (rotation);} @Overridepublic void Onanimationrepeat (Animation Animation) {}}/** * registers the animation listener in the ImageView click Animation, which is used to complete the subsequent animation of ImageView. * * @author Guolin */class Turntolistview implements Animationlistener {@Overridepublic void Onanimationstart (Animation a nimation) {}/** * When the animation of ImageView is complete, you also need to start the ListView animation again, so that the ListView will not be visible to visible */@Overridepublic void Onanimationend ( Animation Animation) {//Get the center point position of the layout as the center point of the rotation float CenterX = layout.getwidth ()/2f;float centery = layout.getheight ()/2f ;//Will ImageView hide picture.setvisibility (view.gone);//The ListView will display piclistview.setvisibility (view.visible); Piclistview.requestfocus ();//Build a 3D rotated animation object with a rotation angle of 90-0 degrees, which will make the ListView invisible and return toOrigin final Rotate3danimation rotation = new Rotate3danimation (0, CenterX, centery,310.0f, false);//animation duration 500 ms rotation . setduration (500);//The State Rotation.setfillafter (true) to remain complete after the animation is complete; Rotation.setinterpolator (new Accelerateinterpolator ()); layout.startanimation (rotation);} @Overridepublic void Onanimationrepeat (Animation Animation) {}}}
Key classes:
Rotate3danimation
/** * An animation, rotates the view on the Y axis between the specified * angles. 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;priva Te final float mcenterx;private final float mcentery;private final float mdepthz;private final Boolean mreverse;private Ca Mera mcamera;/** * Creates a new 3D rotation on the Y axis. The rotation is defined by its * start angle and its end angle. Both angles is in degrees. The rotation * is performed around a center point in the 2D space, definied by a pair of * X and Y coordinates, called CEN Terx and CenterY. When the animation * starts, a translation on the Z axis (depth) is performed. The length of * The translation can is specified, as well as whether the translation * should is 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 * @param reverse * True if the translation should be reversed, false o Therwise */public rotate3danimation (float fromdegrees, float todegrees, float centerx, float centery,float Depthz, Boolea N reverse) {mfromdegrees = Fromdegrees;mtodegrees = Todegrees;mcenterx = Centerx;mcentery = Centery;mdepthz = DepthZ;mReve RSE = reverse;} @Overridepublic void Initialize (int width, int height, int parentwidth, int parentheight) {super.initialize (width, height , Parentwidth, parentheight); Mcamera = new Camera ();} @Overrideprotected 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 (); Depth of field on//z axis 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 ();//camera.rotatey (degrees); the coordinate point that rotates around the y axis is in (0, 0);//In order for it to//pretranslate function around (CenterX, centery) point to move the coordinates (CenterX, centery) to the center point before the Rotatey begins Matrix.pretranslate (- CenterX,-centery)//posttranslate when the Rotatey is executed, the center point is moved back to Matrix.posttranslate (CenterX, CenterY);}}
Layout file:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/layout" Android: Layout_width= "Match_parent" android:layout_height= "match_parent" > <listview android:id= "@+id/pic_l Ist_view "android:layout_width=" match_parent "android:layout_height=" match_parent "> </List view> <linearlayout android:id= "@+id/picture" android:layout_width= "Match_parent" Andro id:layout_height= "Match_parent" android:clickable= "true" android:orientation= "vertical" android:visib Ility= "Gone" > <textview android:layout_width= "match_parent" Android:layout_heig ht= "Wrap_content" android:id= "@+id/text" android:text= "ddddddd"/> <imageview Android:layout_width= "200DP" android:layout_height= "200DP" android:src= "@drawable/bird" /> <button AndroidOid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "ddddddd"/> </LinearLayout></RelativeLayout>
(GO) android3d animation, rotation around the y-axis