Android 属性动画监听事件与一个菜单的例子

简单监听事件

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 {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
	}
    
	public void 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);
        /**
         *添加所有监听事件
         */
//        animator.addListener(new AnimatorListener() {
//			
//			@Override
//			public void onAnimationStart(Animator 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) {
//			}
//		});
        
        /**
         * 按需添加
         */
        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();
	}
     
}

看一个简单的例子

这是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" >

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/image_b" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/image_c" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/image_d" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/image_e" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/image_f" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/image_g" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/image_h" />
    
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/image_a" />
    

</FrameLayout>

这是主程序

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<ImageView> imageViewList = new ArrayList<ImageView>();
	private boolean isOpen = false;

	@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 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);
			// 动画差值器, Interpolator
			// 被用来修饰动画效果,定义动画的变化率,可以使存在的动画效果accelerated(加速),decelerated(减速),repeated(重复),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;
		}
	}

}



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。