MainActiviy
package com.example.animatorlistenerdemo;
import java.util.ArrayList;
import java.util.List;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
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.imageview_a,R.id.imageview_b,
R.id.imageview_c,R.id.imageview_d,R.id.imageview_e,
R.id.imageview_f,R.id.imageview_g,R.id.imageview_h};
private List<ImageView> imageViewList = new ArrayList<ImageView>();
private boolean flag = true;
@Override
protected 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);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageview_a:
if(flag){
startAnim();
}else{
closeAnim();
}
break;
default:
Toast.makeText(MainActivity.this, "click" + v.getId(), Toast.LENGTH_SHORT).show();
break;
}
}
@SuppressLint("NewApi")
private void startAnim(){
for(int i = 0;i<res.length; i++){
ObjectAnimator animator = ObjectAnimator.ofFloat(imageViewList.get(i), "translationY", 0F,i*120);
animator.setDuration(500);
animator.setInterpolator(new BounceInterpolator());//动画的差时器
animator.setStartDelay(i*200);//动画开始执行的一个延时
animator.start();
}
flag = false;
}
@SuppressLint("NewApi")
private void closeAnim() {
for(int i = 0;i<res.length; i++){
ObjectAnimator animator = ObjectAnimator.ofFloat(imageViewList.get(i), "translationY", i*120,0F);
animator.setDuration(500);
animator.setInterpolator(new BounceInterpolator());//动画的差时器
animator.setStartDelay(i*200);//动画开始执行的一个延时
animator.start();
}
flag = true;
}
/*@SuppressLint("NewApi")
public void click(View view){
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 0f,1f);
animator.setDuration(1000);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Toast.makeText(MainActivity.this, "anim end", Toast.LENGTH_SHORT).show();
}
});*/
// animator.addListener(new AnimatorListener() {
//
// @Override
// public void onAnimationStart(Animator animation) {
// Toast.makeText(MainActivity.this, "anim end", Toast.LENGTH_SHORT).show();
// }
//
// @Override
// public void onAnimationRepeat(Animator animation) {
//
// }
//
// @Override
// public void onAnimationEnd(Animator animation) {
//
// }
//
// @Override
// public void onAnimationCancel(Animator animation) {
//
// }
// });
//
//animator.start();
//}
}
activity_maim.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"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageview_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:src="@drawable/b" />
<ImageView
android:id="@+id/imageview_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:src="@drawable/c" />
<ImageView
android:id="@+id/imageview_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:src="@drawable/d" />
<ImageView
android:id="@+id/imageview_e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:src="@drawable/e" />
<ImageView
android:id="@+id/imageview_f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:src="@drawable/f" />
<ImageView
android:id="@+id/imageview_g"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:src="@drawable/g" />
<ImageView
android:id="@+id/imageview_h"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:src="@drawable/h" />
<ImageView
android:id="@+id/imageview_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:src="@drawable/a" />
</FrameLayout>
源码位置:http://pan.baidu.com/s/1hs2lcoc