简体   繁体   English

如何依次播放AnimatorSet的列表?

[英]How to playSequentially a list of AnimatorSet?

I'm trying to make an animation of an imageview that revolves around a circle shape. 我正在尝试制作围绕圆形旋转的imageview的动画。 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. 预先感谢您的帮助。

You can add a listener to each AnimatorSet by using addListener(Animator.AnimatorListener listener) . 您可以使用addListener(Animator.AnimatorListener listener)将侦听器添加到每个AnimatorSet

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: 其中, myListener的定义如下:

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; 除此之外,还需要一些可变的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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM