Animation(三)
继续上篇的animation,本篇用到的是AnimationListener,参考自iteye上的一个朋友的翻牌动画。AnimationListener主要是用来监听animation的,它接受animation的通知,每个通知声明了一个相关的事件。话不多说,上代码:
package com.kevin.animation;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.AnimationUtils;import android.widget.ImageView;public class AnimationDemo extends Activity {private ImageView img;private boolean isFront = false; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); img = (ImageView)findViewById(R.id.imageView1); // bind onclick event img.setOnClickListener(new ImgOnClickListener()); } class ImgOnClickListener implements OnClickListener{@Overridepublic void onClick(View v) {// load back animation configAnimation animation = AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.back_scale);// bind AnimationListeneranimation.setAnimationListener(new AnimationListener() {// Notifies the start of the animation.@Overridepublic void onAnimationStart(Animation animation) {System.out.println("onAnimationStart.");}// Notifies the repetition of the animation.@Overridepublic void onAnimationRepeat(Animation animation) {System.out.println("onAnimationRepeat");}// Notifies the end of the animation.@Overridepublic void onAnimationEnd(Animation animation) {System.out.println("onAnimationEnd");if(isFront){img.setImageResource(R.drawable.back);isFront = false;}else{img.setImageResource(R.drawable.front);isFront = true;}// load front animation configimg.setAnimation(AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.front_scale));}});// invoke animation immediatelyimg.startAnimation(animation);} }}