gpt4 book ai didi

android - 如何顺序播放AnimatorSet的列表?

转载 作者:行者123 更新时间:2023-11-29 01:02:01 25 4
gpt4 key购买 nike

我正在尝试制作围绕圆形旋转的 ImageView 的动画。这是当前有效但不重复具有不同值的动画的代码:

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);
}

评论(“//”)在这里是为了说明我想要创建的内容。预先感谢您的帮助。

最佳答案

您可以使用 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);
}

其中 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();
}

};

除此之外,您还需要一些变量 private int counter; 来遍历列表。在开始第一个动画之前,将其设置为零:

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

开始下一个动画(如果还有的话):

private void startNextAnimation(){
counter++;
if(counter == myList.size()){
return;
}
myList.get(counter).start();
}

关于android - 如何顺序播放AnimatorSet的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50397370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com