Android动画之AnimatorListenerDemo

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值