Android-using Java code to implement property Animation
Android-using Java code to implement property Animation
In addition to defining xml files to set animations, you can also use java code to control animations.
Example:
Layout file:
Main activity:
Package com. xieth. as. againanimdemo; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. view. view; import android. view. animation. translateAnimation; import android. widget. imageView; import android. widget. toast; public class MainActivity2 extends AppCompatActivity {private ImageView 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 = new TranslateAnimation (0,200, 0, 0); // set the display duration of animation. setDuration (1000); imageView. startAnimation (animation );}}
Set events
Mobile
TranslateAnimation animation = new TranslateAnimation (0,200, 0, 0); // you can specify the length of time for display: animation. setDuration (1000); imageView. startAnimation (animation );
Run:
ObjectAnimator.ofFloat(imageView, "translationX", 0F, 200F).setDuration(1000).start();
Run:
You can see that the effect is the same.
Modify the Code:
ObjectAnimator.ofFloat(imageView, "translationY", 0F, 200F).setDuration(1000).start();
This is the Y Direction:
Rotate <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4ncjxwcmugy2xhc3m9 "brush: java;"> ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F).setDuration(1000).start();
Run:
Composite Animation
ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F).setDuration(1000).start(); ObjectAnimator.ofFloat(imageView, "translationX", 0F, 360F).setDuration(1000).start(); ObjectAnimator.ofFloat(imageView, "translationY", 0F, 360F).setDuration(1000).start();
Run:
The three animations are executed simultaneously.
UsePropertyValuesHolder
PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation", 0F, 360F); PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationX", 0F, 200F); PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationY", 0F, 200F); ObjectAnimator.ofPropertyValuesHolder(imageView, p1, p2, p3).setDuration(1000).start();
Run:
UseAnimatorSet
ObjectAnimator a1 = ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F);ObjectAnimator a2 = ObjectAnimator.ofFloat(imageView, "translationX", 0F, 360F);ObjectAnimator a3 = ObjectAnimator.ofFloat(imageView, "translationY", 0F, 360F);AnimatorSet set = new AnimatorSet();set.setDuration(1000);set.playTogether(a1, a2, a3);set.start();
Run:
The effect is the same.
Previously, they were played by colleagues. Now they can be played in order.
set.playSequentially(a1, a2, a3);
Run:
You can see that the image is rotated first, then moved to the X direction, and then to the Y direction.
You can also perform sequential control in this way.
First, execute the movement in both the X and Y directions, and then rotate
set.play(a2).with(a3);set.play(a1).after(a2);
Run: