2

I'm trying to make an animation of an imageview that revolves around a circle shape. Here is the currrent code who works but without repeating the animation with different values:

public void runAnimation(){
    ImageView worldView=findViewById(R.id.imageView);
    int radius=worldView.getHeight()*30/70;
    int centerx = worldView.getLeft()+radius;
    int centery = worldView.getTop()+radius;
    //List<Animator> myList = new ArrayList<>();
    for (int i=0;i<360;i++) {

        /*---I calculate the points of the circle---*/
        int angle= (int) ((i * 2 * Math.PI) / 360);
        int x= (int) (centerx+(radius*Math.cos(angle)));
        int y= (int) (centery+(radius*Math.sin(angle)));

        /*---Here carView is the ImageView that need to turn around the worldView---*/
        ObjectAnimator animatorX =ObjectAnimator.ofFloat(carView, "x",x);
        ObjectAnimator animatorY =ObjectAnimator.ofFloat(carView, "y",y);
        ObjectAnimator animatorR =ObjectAnimator.ofFloat(carView, "rotation", i);

        animatorX.setDuration(500);
        animatorY.setDuration(500);
        animatorR.setDuration(500);

        AnimatorSet animatorSet=new AnimatorSet();
        animatorSet.playTogether(animatorX,animatorY,animatorR);
        animatorSet.start();
        //myList.add(animatorSet);
    }
    //AnimatorSet animatorSet=new AnimatorSet();
    //animatorSet.playSequentially(myList);
}

The commentary ("//") are here to illustrate what I want to create. Thank in advance for your help.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    As the animations have a fixed duration of 500ms and you're playing them together you could simply start the AnimatorSets from that loop with specific delays to make them run continuously. Add this before calling start(): *animator.setStartDelay(i * 500);* – user May 17 '18 at 18:11

1 Answers1

1

You can add a listener to each AnimatorSet by using addListener(Animator.AnimatorListener listener).

for (int i=0;i<360;i++) {
    // ...skipped some lines here...
    AnimatorSet animatorSet=new AnimatorSet();
    animatorSet.playTogether(animatorX,animatorY,animatorR);
    myList.add(animatorSet);
    animatorSet.addListener(myListener);
}

where myListener is defined as follows:

private Animator.AnimatorListener myListener = new Animator.AnimatorListener(){

    @Override
    public void onAnimationStart(Animator animation){
        // no op
    }

    @Override
    public void onAnimationRepeat(Animator animation){
        // no op
    }

    @Override
    public void onAnimationCancel(Animator animation){
        // no op
    }

    @Override
    public void onAnimationEnd(Animator animation){
        startNextAnimation();
    }

};

In addition to that, you need some variable private int counter; to iterate over the list. Before starting the first animation, you set it to zero:

counter = 0;
myList.get(0).start();

To start the next animation (if there is one left):

private void startNextAnimation(){
    counter++;
    if(counter == myList.size()){
        return;
    }
    myList.get(counter).start();
}
Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61