无限循环问题

时间:2011-08-17 16:38:25

标签: android animation loops

我试图让我的动画旋转直到用户点击视图。我已经将动画设置为旋转一次,我希望它继续进行某种循环,而不是使用.clearAnimation,这样动画就不会在单击时停止一半循环。我提出的代码显然不起作用,但我可以想到如何做到这一点!

while(keepSpinning){

    turntable = (ImageView)findViewById(R.id.deck1);     

    RotateAnimation r ; // = new RotateAnimation(ROTATE_FROM, ROTATE_TO);
    r = new RotateAnimation(ROTATE_FROM, ROTATE_TO, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    r.setDuration(5000);
    r.setRepeatCount(0); 

    turntable.startAnimation(r);
    turntable.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            keepSpinning = false;
            return true;
        }
    });
}

1 个答案:

答案 0 :(得分:2)

而不是while(keepSpinning)循环,register an AnimationListenerRotateAnimation。对于动画的每个循环,RotateAnimation将在侦听器上调用onAnimationRepeat(),您可以在那里取消动画。