ObjectAnimator and androidanimator for Android property Animation
I believe that for beginners of Android, I am very interested in the animation effect in Android. Today I will summarize the property animation cases I just learned.
First, just like the Android app, we need to create a project first. For convenience, we only add one ImageView and button to the layout file. The Code is as follows:
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_vertical_margin" android: paddingBottom = "@ dimen/plugin" tools: context = ". mainActivity "> <ImageView android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: id =" @ + id/imageView "android: layout_alignParentTop =" true "android: layout_alignParentLeft = "true" android: layout_alignParentStart = "true" android: src = "@ drawable/ic_launcher" android: onClick = "imgClick"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Animation" android: id = "@ + id/button" android: layout_alignParentBottom = "true" android: layout_centerHorizontal = "true" android: layout_marginBottom = "86dp" android: onClick = "buttonClick"/> </RelativeLayout>
The following is our action. To facilitate your learning, I will share the Code as follows:
Public class MainActivity extends Activity {public ImageView imageView; public Button button; static int x = 0, xx = 0, y = 0, yy = 0; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); imageView = (ImageView) findViewById (R. id. imageView); button = (Button) findViewById (R. id. button);} public void imgClick (View view) {Toast. makeText (this, "ImageView", Toast. LENGTH_SHORT ). show ();} public void buttonClick (View view) {// xx + = 20; // TranslateAnimation ta = new TranslateAnimation (x, xx, y, yy ); // set the offset of the animation. // x + = 20; // ta. setDuration (1000); // set the animation duration // ta. setFillAfter (true); // sets the position where the animation stops. // imageView. startAnimation (ta); // after the property animation calls the start () method, it is an Asynchronous Operation // ObjectAnimator. ofFloat (imageView, "translationX", 0F, 360F ). setDuration (1000 ). start (); // X-axis Pan rotation // ObjectAnimator. ofFloat (imageView, "translationY", 0F, 360F ). setDuration (1000 ). start (); // y-axis Pan rotation // ObjectAnimator. ofFloat (imageView, "rotation", 0F, 360F ). setDuration (1000 ). start (); // 360 degree rotation // synchronous animation design // PropertyValuesHolder p1 = PropertyValuesHolder. ofFloat ("translationX", 0, 360F); // PropertyValuesHolder p2 = PropertyValuesHolder. ofFloat ("translationY", 0, 360F); // PropertyValuesHolder p3 = PropertyValuesHolder. ofFloat ("rotation", 0, 360F); // ObjectAnimator. ofPropertyValuesHolder (imageView, p1, p2, p3 ). setDuration (1000 ). start (); // use AnimatiorSet to design multiple attribute animations that are synchronously executed. ObjectAnimator animator1 = ObjectAnimator. ofFloat (imageView, "translationX", 0F, 360F); // the X axis Pan rotates ObjectAnimator animator2 = ObjectAnimator. ofFloat (imageView, "translationY", 0F, 360F); // the Y axis Pan rotates ObjectAnimator animator3 = ObjectAnimator. ofFloat (imageView, "rotation", 0F, 360F); // 360 degree rotation AnimatorSet = new AnimatorSet (); // set. playSequentially (animator1, animator2, animator3); // step-by-step execution // set. playTogether (animator1, animator2, animator3); // synchronous execution // sequential control of the execution of the attribute animation // The first synchronous execution of the animation animator2 and animator3, and then the execution of the animation 1 set. play (animator3 ). with (animator1); set. play (animator2 ). after (animator3); set. setduration( 1000); set. start ();}}
I have commented on the key points in detail. You can copy them to your project for testing. I believe you will be able to master the property animation knowledge in Android.