Infinite loop problem
I am trying to get my animation to spin until the user clicks on the view. I have set the animation to spin once and I want it to kee开发者_JS百科p going on some kind of loop instead of using .clearAnimation so that the animation doesnt just stop half way through a cycle when clicked. The code I have come up with so obviously won't work but I can think of how to do it!
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;
}
});
}
Instead of the while(keepSpinning)
loop, register an AnimationListener with the RotateAnimation
. The RotateAnimation
will invoke onAnimationRepeat() on your listener for each loop of the animation, and you can cancel the animation there.
精彩评论