In addition to defining animations directly in Java code, you can use XML files to define animations for reuse.
If you want to use XML to animate, first create a new animator folder under the Res directory, and all the property animation XML files should be stored in this folder. Then in the XML file we can use the following three kinds of tags:
<animator> valueanimator in the corresponding code
<objectAnimator> objectanimator in the corresponding code
<set> animatorset in the corresponding code
To set up animations using XML
1. Defining animations
2. Loading animations using Animatorinflater.loadanimator
3. Use Settarget to set the component you want to animate
4.start Animation
For example, we want to change the transparency of a picture from 0 to 1.
Res/animator/image_alpha.xml
<?XML version= "1.0" encoding= "Utf-8"?><Objectanimatorxmlns:android= "Http://schemas.android.com/apk/res/android"android:duration= " the"Android:propertyname= "Alpha"Android:valuefrom= "0"Android:valueto= "1"Android:valuetype= "Floattype" ></Objectanimator>
Mainactivity.java
PackageCn.lixyz.animator;ImportAndroid.animation.Animator;ImportAndroid.animation.AnimatorInflater;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateImageView IV; PrivateButton BT; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); } Private voidInitview () {IV=(ImageView) Findviewbyid (r.id.image); BT=(Button) Findviewbyid (R.ID.BT); Bt.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.bt:animator Anim= Animatorinflater.loadanimator ( This, R.animator.image_alpha); Anim.settarget (iv); Anim.start (); Break; } }}
Objectanimator has the following properties to choose from
Use <set> tags to assemble animations
For example, we want to make the picture move from the left to the screen while tumbling:
Res/animator/image_alpha.xml
<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android"android:ordering= "Together" > <Objectanimatorandroid:duration= " the"Android:propertyname= "Translationx"Android:valuefrom= " -500"Android:valueto= "0"Android:valuetype= "Floattype" > </Objectanimator> <Objectanimatorandroid:duration= " the"Android:propertyname= "Rotation"Android:repeatcount= "3"Android:valuefrom= "0"Android:valueto= " the"Android:valuetype= "Floattype" > </Objectanimator></Set>
Mainactivity.java
PackageCn.lixyz.animator;ImportAndroid.animation.Animator;ImportAndroid.animation.AnimatorInflater;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateImageView IV; PrivateButton BT; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); } Private voidInitview () {IV=(ImageView) Findviewbyid (r.id.image); BT=(Button) Findviewbyid (R.ID.BT); Bt.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.bt:animator Anim= Animatorinflater.loadanimator ( This, R.animator.image_alpha); Anim.settarget (iv); Anim.start (); Break; } }}
In the Set tab, we use ordering to set whether multiple animations are performed at the same time or at once, and this tag has two values to choose from, as the name implies
Together: At the same time
Sequentially: In turn
Transfer from Guo Lin blog: http://blog.csdn.net/guolin_blog/article/details/43536355
Android Note (66) animation in Android--xml file definition property animation