如何在循环中设置动画

时间:2014-10-16 10:41:46

标签: java android animation

我尝试在Android中使用动画,并且我使用以下代码来运行动画:

final ImageView img = (ImageView) findViewById(R.id.imageView2);
        img.setBackgroundResource(R.anim.animation);
        img.setImageDrawable(null);
        AnimationDrawable anim = (AnimationDrawable) img.getBackground();    
        anim.start();

但是,这个动画只运行一次!我该如何无限地运行它?

1 个答案:

答案 0 :(得分:1)

尝试使用以下代码:

ImageView imgview= new ImageView(this);
imgview.setImageResource(R.drawable.starlight);
final AnimatorSet animate = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.anim.anim_rotate_fade);
AnimatorListenerAdapter animatorListener = new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        new Handler().postDelayed(new Runnable() {
            @Override public void run() {
                animate.start();
            }
        }, 1000);
    }
};
animate.setTarget(imgview);
animate.addListener(animatorListener);