Here to share an interesting animation: here is more suitable for a picture of the flip, if it is more than one picture, you can refer to the example in Apidemo, is to add a arrayadapter, or simple, you can make their own changes to achieve their own needs. The code here can basically run the project directly.
Add a ImageView in main.xml, such as
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<framelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/container"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<imageview
Android:id= "@+id/image"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Rotate"
Android:textsize= "50px"
android:layout_x= "150px"
Android:layout_y= "30px"
android:src= "@drawable/ro" >
></ImageView>
</FrameLayout>
It doesn't have to be explained, it can be read.
Finally, you need an activity class
Such as:
Copy Code code as follows:
public class Testrotate extends activity implements onclicklistener{
Private Mageview ImageView;
Private ViewGroup Mcontainer;
/**
* This variable is set to the picture, if it is more than one picture, then you can use an array, such as
*private static final int IMAGE = new int[]{
* r.drawable.ro,
* R.drawable.icon
*};
* How many pictures are put on how much, I do here is only a picture of the flip
*
*/
private static final int IMAGE = r.drawable.ro;
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
ImageView = (ImageView) Findviewbyid (r.id.image);
Mcontainer = (viewgroup) Findviewbyid (R.id.container);
/**
* Set up the most recently displayed picture
* If it is an array, then it can be written as Image[int]
*
*/
Imageview.setimageresource (IMAGE);
/**
*
* Set the ImageView Onclicklistener
*
*/
Imageview.setclickable (TRUE);
Imageview.setfocusable (TRUE);
Imageview.setonclicklistener (this);
}
private void applyrotation (int position, float start, float end) {
Find the center of the container
Final float CenterX = mcontainer.getwidth ()/2.0f;
Final float centery = mcontainer.getheight ()/2.0f;
Final Rotate3d rotation =
New Rotate3d (Start, End, CenterX, CenterY, 310.0f, true);
Rotation.setduration (500);
Rotation.setfillafter (TRUE);
Rotation.setinterpolator (New Accelerateinterpolator ());
Rotation.setanimationlistener (new Displaynextview (position));
Mcontainer.startanimation (rotation);
}
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
/**
*
* Call this method, is to flip the picture
* The parameters are very simple, everyone should understand
* Simply put, the first is the position, the second is the beginning of the angle, the third is the angle of the end
* Here's what you need to say, if you're going back to the last one
* Set the first parameter to-1 on the line
*
*/
Applyrotation (0,0,90);
}
Private Final class Displaynextview implements Animation.animationlistener {
private final int mposition;
Private Displaynextview (int position) {
Mposition = position;
}
public void Onanimationstart (Animation Animation) {
}
public void Onanimationend (Animation Animation) {
Mcontainer.post (New Swapviews (mposition));
}
public void Onanimationrepeat (Animation Animation) {
}
}
/**
* This class are responsible for swapping the views and start the second
* Half of the animation.
*/
Private Final class Swapviews implements Runnable {
private final int mposition;
public swapviews (int position) {
Mposition = position;
}
public void Run () {
Final float CenterX = mcontainer.getwidth ()/2.0f;
Final float centery = mcontainer.getheight ()/2.0f;
Rotate3d rotation;
if (Mposition >-1) {
Imageview.setvisibility (view.visible);
Imageview.requestfocus ();
rotation = new Rotate3d (180, CenterX, CenterY, 310.0f, false);
} else {
Imageview.setvisibility (View.gone);
rotation = new Rotate3d (0, CenterX, CenterY, 310.0f, false);
}
Rotation.setduration (500);
Rotation.setfillafter (TRUE);
Rotation.setinterpolator (New Decelerateinterpolator ());
Mcontainer.startanimation (rotation);
}
}
}