简体   繁体   English

循环内有AnimationListener吗? (等待动画每次完成)

[英]AnimationListener inside of a loop? (Wait for animation to complete each time)

UPDATE: To make my question more clear: How should I structure the loop, to ensure that the next iteration doesn't happen until the animation has ended for the previous iteration? 更新:让我的问题更加清楚:我应该如何构造循环,以确保在上一次迭代的动画结束之前不会发生下一次迭代?

So I'm pretty new to using animations in android. 因此,我在android中使用动画非常陌生。 I've got simple animation that runs each time a tile is placed using the placeTile method. 我有一个简单的动画,每次使用placeTile方法放置placeTile贴时都会运行。

The code looks like this: 代码如下:

public void setUpPlayers(){

    //Toast for setting turn order
    smallToast(getResources().getString(R.string.deciding_turn_order));

    for (int i = 0; i < players.length; i++) {
        if(i == 0){
            players[i] = new Player("HUMAN", 6000);
            players[i].setType(Player.Type.HUMAN);
        } else {
            players[i] = new Player(playerNames[i], 6000);
            players[i].setType(Player.Type.COMPUTER);
        }           
        players[i].drawTile(1);

        TextView nameText = (TextView) findViewById(R.id.current_player_text);
        nameText.setText(players[i].getName());

        //find the ID for the tile just placed
        String tileID = players[i].findTileIdByIndex(0);

        placeTile(tileID, i); //Tile ID being placed, and the index of the player placing it
    }
}

The issue I have, is I want the loop to NOT execute the next iteration until the animation has completed for the prior tile placed. 我的问题是,我希望循环在动画完成之前放置的图块之前不执行下一个迭代。 How can I do this? 我怎样才能做到这一点? I know that typically you have to set an AnimationLister and wait for the animation to finish but I'm not sure how to go about doing that since A) the animation is part of the Tile being placed and has a private scope, and B) the loop wants to execute immediately. 我知道通常您必须设置一个AnimationLister并等待动画完成,但是我不确定该怎么做,因为A)动画是所放置的Tile的一部分,并且具有私有作用域,而B)循环要立即执行。 How can you "Hold" a loop using a Listener? 如何使用侦听器“保留”循环?

The one thought that occurred to me would be to re-structure the setUpPlayers() method to be a recursive algorithm and each iteration calls the animation, but the reiteration of the method is called inside of the AnimationListener. 我想到的一个想法是将setUpPlayers()方法重新构造为一种递归算法,每次迭代都调用动画,但是该方法的重复在AnimationListener内部调用。 A recursive algorithm seems like overkill for this though. 虽然如此,递归算法似乎有点过头了。 Any thoughts? 有什么想法吗? What's the best, simplest method to achieve this? 什么是最好,最简单的方法来实现这一目标?

Thanks in advance, 提前致谢,

JRad the Bad 坏家伙

In my project I have a similar situation and I also set my min sdk to 8 so I used this library to have the same features we have on the latests updates (Animator classes) 在我的项目中,我遇到了类似的情况,并且还将我的min sdk设置为8,所以我使用了该库来具有与最新更新(Animator类)相同的功能。

Library - http://nineoldandroids.com/ 图书馆-http://nineoldandroids.com/

My code: 我的代码:

    private class MyAnimationListener implements Animator.AnimatorListener{

                ObjectAnimator spinningAnimation;

                @Override
                public void onAnimationStart(Animator animation) {
                    spinningAnimation = ObjectAnimator.ofFloat(playerSpinner, "rotationY", degrees); //In my case I just flip a coin
                    spinningAnimation.setDuration(25);
                    spinningAnimation.addListener(new MyAnimationListener());
                }

               @Override    
               public void onAnimationEnd(Animator animation) {
                   if(repeatCount>0){
                        spinningAnimation.setTarget(playerSpinner);
                        spinningAnimation.start();
                   }
               }

               @Override
               public void onAnimationCancel(Animator animation) {}

               @Override
               public void onAnimationRepeat(Animator animation) {}

}

If you need some help to understand better, just ask on the comments :) 如果您需要一些帮助以更好地理解,请在评论中提问:)

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

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