This article illustrates the method of 3D sliding rotation effect of Android programming. Share to everyone for your reference, specific as follows:
Here we implement some sliding page animation effects through code.
Animation animations are implemented in two ways: Frame animation (Frame-by-frame animation) and motion tweens (tweened animation)
This example implements the 3D paging effect by inheriting animation custom Rotate3d. The effect chart is as follows:
1, Rotate3d (Animation)
First, customize the animation 3D animation class Rotate3d
public class Rotate3d extends Animation {private float fromdegree; Rotation starting angle private float todegree; Rotary Termination angle private float Mcenterx; Rotary Center x private float Mcentery;
Rotary Center y private Camera Mcamera;
Public Rotate3d (float fromdegree, float todegree, float centerx, float centery) {this.fromdegree = Fromdegree;
This.todegree = Todegree;
This.mcenterx = CenterX;
This.mcentery = CenterY; @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 Fromdegr
EE = fromdegree; Float degrees = fromdegree + (todegree-fromdegree) * interpolatedtime;
Rotation angle (angle) Final float CenterX = Mcenterx;
Final float centery = mcentery;
Final Matrix matrix = T.getmatrix (); if (degreEs <= -76.0f) {degrees = -90.0f;
Mcamera.save (); Mcamera.rotatey (degrees);
Rotating Mcamera.getmatrix (matrix);
Mcamera.restore ();
else if (degrees >= 76.0f) {degrees = 90.0f;
Mcamera.save ();
Mcamera.rotatey (degrees);
Mcamera.getmatrix (matrix);
Mcamera.restore ();
else {mcamera.save (); Mcamera.translate (0, 0, CenterX);
Displacement x mcamera.rotatey (degrees);
Mcamera.translate (0, 0,-centerx);
Mcamera.getmatrix (matrix);
Mcamera.restore ();
} matrix.pretranslate (-centerx,-centery);
Matrix.posttranslate (CenterX, centery);
}
}
Then instantiate the rotation direction of the Rotate3d
public void Initanimation () {//Get rotation center displaymetrics DM = new Displaymetrics ();
DM = Getresources (). Getdisplaymetrics ();
Mcenterx = DM.WIDTHPIXELS/2;
Mcentery = DM.HEIGHTPIXELS/2;
Defines the direction of rotation int duration = 1000; Lquest1animation = new Rotate3d (0, -90, Mcenterx, mcentery);
The "Question1" rotation direction of the next page (from 0 degrees to 90, the reference system is 0 degrees horizontally) lquest1animation.setfillafter (true);
Lquest1animation.setduration (duration); Lquest2animation = new Rotate3d (0, Mcenterx, mcentery);
The "Question2" rotation direction of the next page (from 90 degrees to 0, the reference line to 0 degrees) (the first question) Lquest2animation.setfillafter (true);
Lquest2animation.setduration (duration); Rquest1animation = new Rotate3d (0, Mcenterx, mcentery);
The "Question1" rotation direction of the previous page (from 0 degrees to 90, the reference system is 0 degrees horizontally) rquest1animation.setfillafter (true);
Rquest1animation.setduration (duration); Rquest2animation = new Rotate3d ( -90, 0, Mcenterx, mcentery);
The "Question2" rotation direction of the previous page (from 90 degrees to 0, the reference system is 0 degrees horizontally) rquest2animation.setfillafter (true); Rquest2animation.setduration (duration);
}
2. Activity
First, define two layout files to rotate the screen switch
Main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:id= "@+id/layout_main"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:orientation= " Vertical "> ...
</LinearLayout>
Next.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:id= "@+id/layout_next"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:orientation= " Vertical "> ...
</LinearLayout>
Limited to space, complete layout file please see source code ^_^
Then, initialize the two rotated layout file resources
private void Initmain () {
setcontentview (r.layout.main);
Layoutmain = (linearlayout) Findviewbyid (r.id.layout_main);
Btn_mainlast = (Button) Findviewbyid (r.id.main_last);
Btn_mainnext = (Button) Findviewbyid (r.id.main_next);
Btn_mainlast.setonclicklistener (listener);
Btn_mainnext.setonclicklistener (listener);
}
private void Initnext () {
setcontentview (r.layout.next);
Layoutnext = (linearlayout) Findviewbyid (r.id.layout_next);
Btn_nextlast = (Button) Findviewbyid (r.id.next_last);
Btn_nextnext = (Button) Findviewbyid (r.id.next_next);
Btn_nextlast.setonclicklistener (listener);
Btn_nextnext.setonclicklistener (listener);
}
Finally, set the button listener event in the layout file to respond to the 3D rotation animation and direction
Private View.onclicklistener listener = new View.onclicklistener () {
@Override public
void OnClick (View v) {
switch (V.getid ()) {case
r.id.main_last: //prev
layoutmain.startanimation (lquest1animation); The current page rotates to the left (0,-90)
initnext ();
Layoutnext.startanimation (lquest2animation); The next page rotates to the left (0)
;
Case R.id.main_next: //Next page
layoutmain.startanimation (rquest1animation); The current page rotates to the right (0,90)
initnext ();
Layoutnext.startanimation (rquest2animation); The next page rotates to the right ( -90, 0)
;
Case R.id.next_last:
layoutnext.startanimation (lquest1animation);
Initmain ();
Layoutmain.startanimation (lquest2animation);
break;
Case R.id.next_next:
layoutnext.startanimation (rquest1animation);
Initmain ();
Layoutmain.startanimation (rquest2animation);
break;
}
}
;
Full instance code code click here to download the site.
I hope this article will help you with your Android programming.