我正在Andorid中为动画尝试以下代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lay);
rel = (LinearLayout) findViewById(R.id.rel);
rel.setBackgroundDrawable(getResources().getDrawable(R.drawable.gradient_background));
animation = new AlphaAnimation(1, 0);
animation.setDuration(500);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(3);
rel.startAnimation(animation);
animation1 = new AlphaAnimation(1, 0);
animation1.setDuration(1500);
animation1.setInterpolator(new LinearInterpolator());
animation1.setRepeatCount(3);
animationchala();
}
public void animationchala(){
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
animation.setAnimationListener(null);
rel.startAnimation(animation1);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
animation1.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation1) {
}
public void onAnimationEnd(Animation animation1) {
animation1.setAnimationListener(null);
rel.startAnimation(animation);
}
@Override
public void onAnimationRepeat(Animation animation1) {
}
});
}
代码第一次有效。
当动画结束时动画1开始,然后动画1结束并再次启动动画,但是现在当动画1必须再次开始它停止时,我怎么能一个接一个地重复动画
答案 0 :(得分:1)
问题在于您在启动新动画时将animationListeners
设置为null
。这使得它们在您开始动画后停止。只需像这样重新安排您的代码;
public void animationchala(){
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
rel.startAnimation(animation1);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
animation1.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation1) {
}
public void onAnimationEnd(Animation animation1) {
rel.startAnimation(animation);
}
@Override
public void onAnimationRepeat(Animation animation1) {
}
});
}