Simple Android image flip animation implementation code

Source: Internet
Author: User
Tags image flip

The following is an interesting Animation: It is suitable for the flip of an image. If there are multiple images, you can refer to the example in APIDemo to add an ArrayAdapter, which is simple, you can also make modifications to achieve what you want. The code here can basically run the project directly.
Add an ImageView to main. xml, as shown in figure

Copy codeThe Code is 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>

You can understand this.
Finally, an activity class is required.
For example:Copy codeThe Code is as follows: public class TestRotate extends Activity implements OnClickListener {
Private mageView imageview;
Private ViewGroup mContainer;
/**
* This variable sets the image. If there are multiple images, you can use an array, as shown in figure
* Private static final int IMAGE = new int [] {
* R. drawable. ro,
* R. drawable. icon
*};
* If you have images, you can only flip them.
*
*/
Private static final int IMAGE = R. drawable. ro;
/** Called when the activity is first created .*/
@ 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 the latest image.
* If it is an array, you can write it as IMAGE [int].
*
*/
Imageview. setImageResource (IMAGE );
/**
*
* Set the OnClickListener of ImageView.
*
*/
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 to flip an image.
* The parameter is very simple and everyone should understand it.
* To put it simply, the first is the position, the second is the start angle, and the third is the end angle.
* It should be noted that, if you want to return to the previous
* Set the first parameter to-1.
*
*/
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 is 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 (90,180, centerX, centerY, 310.0f, false );
} Else {
Imageview. setVisibility (View. GONE );
Rotation = new Rotate3d (90, 0, centerX, centerY, 310.0f, false );
}
Rotation. setDuration (500 );
Rotation. setFillAfter (true );
Rotation. setInterpolator (new DecelerateInterpolator ());
MContainer. startAnimation (rotation );
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.