动画监听器
我们画了那么都能动的图,但是在它动的过程中我们只能袖手旁观,能不能对动画的状态加一些
监听呢,android.view.animation.Animation.AnimationListener;动画监听器,现在对我们上节课所学的平移动画做监听。
监听三种:
public void onAnimationEnd(Animation arg0) //结束
public void onAnimationRepeat(Animation arg0) //重复
public void onAnimationStart(Animation arg0) //开始
现在我们完成一个实例,实现动画完成以后移除组件,也就是图画。
代码如下:
public class MainActivity extends Activity {
ImageView image;
ViewGroup group;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image=(ImageView)super.findViewById(R.id.image);
group=(ViewGroup)super.findViewById(R.layout.activity_main);
}
public void begin(View v){
AnimationSet set=new AnimationSet(true);
TranslateAnimation trans=new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0.0f,
Animation.RELATIVE_TO_SELF,0.5f,//以自身0.5宽度为轴
Animation.RELATIVE_TO_SELF,0.0f,
Animation.RELATIVE_TO_SELF,1.5f);//以y轴原点进行计算
trans.setRepeatCount(3);
trans.setInterpolator(new AccelerateDecelerateInterpolator());
set.addAnimation(trans);//增加动画
set.setDuration(3000);//三秒完成动画
set.setAnimationListener(new AnimationListener(){
@Override
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
MainActivity.this.group.removeView(image);
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
MainActivity.this.image.startAnimation(set);
}
}
现在我们在开始那部分设置渐变消失动态,加入以下代码就好:
if(animation instanceof AnimationSet){
AnimationSet set=(AnimationSet)animation;
AlphaAnimation a=new AlphaAnimation(1,0);
a.setDuration(3000);
set.addAnimation(a);
}
现在我们看看效果:
看到这里是不是有点小兴奋啊,别着急,把这一章学好你就能做一个小游戏或充满意义的小动画了啦。