首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何playSequentially一个AnimatorSet列表?

如何playSequentially一个AnimatorSet列表?
EN

Stack Overflow用户
提问于 2018-05-17 17:32:06
回答 1查看 962关注 0票数 2

我试着制作一个图像视图的动画,它围绕着一个圆形的形状。下面是运行但不重复具有不同值的动画的当前代码:

代码语言:javascript
复制
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);
}

这里的评注("//")是为了说明我想要创造的东西。提前感谢您的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-17 18:26:20

您可以通过使用AnimatorSet向每个addListener(Animator.AnimatorListener listener)添加一个侦听器。

代码语言:javascript
复制
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的定义如下:

代码语言:javascript
复制
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;来迭代列表。在启动第一个动画之前,将其设置为零:

代码语言:javascript
复制
counter = 0;
myList.get(0).start();

要启动下一个动画(如果还剩一个):

代码语言:javascript
复制
private void startNextAnimation(){
    counter++;
    if(counter == myList.size()){
        return;
    }
    myList.get(counter).start();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50397370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档