Android property animation listening event and a menu example
Simple event monitoring
Package com. example. animation; import android. animation. animator; import android. animation. animator. animatorListener; import android. animation. animatorListenerAdapter; import android. animation. animatorSet; import android. animation. objectAnimator; import android. animation. propertyValuesHolder; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import Android. view. view; import android. view. animation. animation; import android. view. animation. animationSet; import android. view. animation. translateAnimation; import android. widget. imageView; import android. widget. toast; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public vo Id click (View view) {Toast. makeText (this, "click", Toast. LENGTH_SHORT ). show ();} public void move (View view) {ImageView imageView = (ImageView) findViewById (R. id. imageView); ObjectAnimator animator = new ObjectAnimator (). ofFloat (imageView, "translationX", 0f, 200f); animator. setDuration (1000);/*** add all listening events * // animator. addListener (new AnimatorListener () {// @ Override // public void onAnimationStart (Anima Tor arg0) {//} // @ Override // public void onAnimationRepeat (Animator arg0) {//} // @ Override // public void onAnimationEnd (Animator arg0) {// Toast. makeText (MainActivity. this, "animator is end! ", Toast. LENGTH_SHORT ). show (); //} // @ Override // public void onAnimationCancel (Animator arg0 ){//}//}); /*** add as needed */animator. addListener (new AnimatorListenerAdapter () {@ Override public void onAnimationEnd (Animator animation) {// TODO Auto-generated method stub super. onAnimationEnd (animation); Toast. makeText (MainActivity. this, "animator is end! ", Toast. LENGTH_SHORT). show () ;}}); animator. start ();}}
Let's look at a simple example.
This is xml
<frameLayout 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.example.animator.MainActivity" >
</frameLayout>
This is the main program
Package com. example. animator; import java. util. arrayList; import java. util. list; import android. animation. objectAnimator; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. view. animation. bounceInterpolator; import android. widget. imageView; import android. widget. toast; public class MainActivity extends Activity implements OnClickListener {private int [] res = {R. id. imageView1, R. id. imageView2, R. id. imageView3, R. id. imageView4, R. id. imageView5, R. id. imageView6, R. id. imageView7, R. id. imageView8}; private List
ImageViewList = new ArrayList
(); Private boolean isOpen = false; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); for (int I = 0; I <res. length; I ++) {ImageView imageView = (ImageView) findViewById (res [I]); imageView. setOnClickListener (this); imageViewList. add (imageView) ;}@overridepublic void onClick (View view) {switch (view. getId () {case R. id. ImageView1: if (! IsOpen) {startAnim () ;}else {closeAnim () ;}break; default: Toast. makeText (this, "you click item! ", Toast. LENGTH_SHORT ). show (); break ;}} private void startAnim () {for (int I = 1; I <res. length; I ++) {ObjectAnimator animator = ObjectAnimator. ofFloat (imageViewList. get (I), "translationY", 0f, I * 150); animator. setDuration (500); // animation viewer, Interpolator // used to modify the animation effect, define the animation change rate, and enable the existing animation effect accelerated (acceleration ), decelerated, repeated, and bounced. Animator. setInterpolator (new BounceInterpolator (); animator. setStartDelay (I * 350); animator. start (); isOpen = true ;}} private void closeAnim () {for (int I = 1; I <res. length; I ++) {ObjectAnimator animator = ObjectAnimator. ofFloat (imageViewList. get (I), "translationY", I * 150, 0f); animator. setDuration (500); animator. setInterpolator (new BounceInterpolator (); animator. setStartDelay (I * 350); animator. start (); isOpen = false ;}}}