Search code examples
androidrotateanimation

Android Animated Turns


I'm using the following code to make turns (left or right) in a series. So if i call it one after another ie: turn(90); turn(90); turn(-90); all it shows is the last one. I'd like to show them all and it wait until the first one is done before moving on to the next. Any idea's?

public void turn(int i)
{

    RotateAnimation anim = new RotateAnimation( currentRotation, currentRotation + i,
                                                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
                                                currentRotation = (currentRotation + i) % 360;

    anim.setInterpolator(new LinearInterpolator());
    anim.setDuration(1000);
    anim.setFillEnabled(true);

    anim.setFillAfter(true);
    token.startAnimation(anim);
}

Solution

  • So what i did was create a que of the animations and an iterator to run through it... after i defined all the animations that needed to be done, added them to the que, i ran the first one, then within the AnimationListener handled the rest.

    Queue<RotateAnimation> que = new LinkedList<RotateAnimation>();
    Iterator<RotateAnimation> queIt;
    
    
    public void turnToken(int i){
    
        RotateAnimation anim = new RotateAnimation( currentRotation, currentRotation + i,
                                                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
                                                    currentRotation = (currentRotation + i) % 360;
    
        anim.setInterpolator(new LinearInterpolator());
        anim.setDuration(1000);
        anim.setFillEnabled(true);
        anim.setAnimationListener(this);
        anim.setFillAfter(true);
    
        que.add(anim);
    }
    

    AnimationListener:

    @Override
    public void onAnimationEnd(Animation arg0) {
        // TODO Auto-generated method stub
    
        if(queIt.hasNext()){
            token.startAnimation((Animation) queIt.next());
            }
    }
    
    @Override
    public void onAnimationRepeat(Animation arg0) {
        // TODO Auto-generated method stub
    }
    
    @Override
    public void onAnimationStart(Animation arg0) {
        // TODO Auto-generated method stub
    
    }