Android-using Java code for property animation
In addition to being able to set animations using the definition XML file. You can also use Java code to control animations.
The scale is as follows:
Layout file:
<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:paddingbottom="@dimen/activity_vertical_margin"android:paddingleft="@dimen/activity_horizontal_margin"android:paddingright="@dimen/activity_horizontal_margin"android:paddingtop="@dimen/activity_vertical_margin"tools:context="Com.xieth.as.againanimdemo.MainActivity2"> <imageview android:id="@+id/id_img"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:onclick="click"Android:src="@mipmap/ic_launcher"/> <button android:id="@+id/id_btnmove"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"Android:layout_alignparentbottom="true"android:onclick="Move"Android:layout_centerhorizontal="true"Android:layout_marginbottom="40DP"android:text="Move"/></relativelayout>
Main activities:
PackageCom.xieth.as.againanimdemo;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.view.animation.TranslateAnimation;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast; Public class MainActivity2 extends appcompatactivity { PrivateImageView ImageView =NULL;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.activity_main2); ImageView = (ImageView) Findviewbyid (r.id.id_img); } Public void Click(View view) {Toast.maketext ( This,"click", Toast.length_short). Show (); } Public void Move(View view) {Translateanimation animation =NewTranslateanimation (0, $,0,0);//Set the length of the displayAnimation.setduration ( +); Imageview.startanimation (animation); }}
Setting events
Move
new TranslateAnimation(020000);// 设置显示的时长animation.setDuration(1000);imageView.startAnimation(animation);
Perform:
ObjectAnimator.ofFloat"translationX"0200F).setDuration(1000).start();
Perform:
Can see the effect is the same.
Change the code:
ObjectAnimator.ofFloat"translationY"0200F).setDuration(1000).start();
This is the y direction:
Rotating
ObjectAnimator.ofFloat"rotation"0360F).setDuration(1000).start();
Perform:
Combo Animations
Objectanimator. Offloat(ImageView,"Rotation",0F theF. Setduration( +). Start();Objectanimator. Offloat(ImageView,"Translationx",0F theF. Setduration( +). Start();Objectanimator. Offloat(ImageView,"Translationy",0F theF. Setduration( +). Start();
Perform:
Be able to see these three animations executed at the same time
Using propertyvaluesholder
Propertyvaluesholder P1 = Propertyvaluesholder. Offloat("Rotation",0F theF;Propertyvaluesholder P2 = Propertyvaluesholder. Offloat("Translationx",0F $F;Propertyvaluesholder P3 = Propertyvaluesholder. Offloat("Translationy",0F $F;Objectanimator. Ofpropertyvaluesholder(ImageView, p1, p2, p3). Setduration( +). Start();
Perform:
Using animatorset
"rotation"0F360F"translationX"0F360F"translationY"0F360Fsetnew AnimatorSet();set.setDuration(1000);set.playTogether(a1, a2, a3);set.start();
Perform:
The same effect.
Were previously played by colleagues. can now be executed in order.
set.playSequentially(a1, a2, a3);
Perform:
Can see the first rotation, and then move in the x direction. Move in the Y direction again.
This can also be controlled sequentially.
Move the x and y directions at the same time, then rotate
set.play(a2).with(a3);set.play(a1).after(a2);
Perform:
Android-using Java code for property animation