Android-using Java code to implement property Animation

Source: Internet
Author: User

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:

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.