简体   繁体   English

AnimatorSet 在顺序播放时停止

[英]AnimatorSet stopping when playing sequentially

I have some code which creates a list of points called path.我有一些代码可以创建一个名为 path 的点列表。

Here is a sample list: [[1, 3], [2, 3], [2, 4]]这是一个示例列表:[[1, 3], [2, 3], [2, 4]]

Here is the code I wrote to animate movement to these points sequentially:这是我编写的用于按顺序为这些点设置动画的代码:

        ArrayList<Animator> movements = new ArrayList<>();
        int prevY = start[1];
        int prevX = start[0];
        for (int[] square : path) {

            ObjectAnimator motion = new ObjectAnimator();

            float[] coord = screenFromGrid(square[0], square[1], gridSizeY);
            long speed = (long) (6000.0 / this.displayMetrics.widthPixels);
            speed = (long) (speed * getSquareSize(gridSizeY));

            if (square[0] != prevX) {

                motion = ObjectAnimator.ofFloat(this.sprite1, "x", coord[0]);
                motion.setInterpolator(new LinearInterpolator());
                motion.setDuration(speed);
                movements.add(motion);
                prevX = square[0];
            } else if (square[1] != prevY) {

                motion = ObjectAnimator.ofFloat(this.sprite1, "y", coord[1]);
                motion.setInterpolator(new LinearInterpolator());
                motion.setDuration(speed);
                movements.add(motion);
                prevY = square[1];
            }
        }
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playSequentially(movements);
        animatorSet.start();

The problem is that animation completely stops in the middle without throwing any error.问题是动画完全停在中间而不会抛出任何错误。

I checked the debugger and it says all the animations have been stored properly.我检查了调试器,它说所有动画都已正确存储。

Does anyone know why this is happening?有谁知道为什么会这样?

All I know from experience is, that AnimatorSet is buggy, and does not always work as expected, I have also had my issues with it, and luckily found another, better approach : ViewPropertyAnimator is a nice way to animate Views with short and concise code.我从经验中所知道的是,AnimatorSet 有问题,并且并不总是按预期工作,我也遇到了我的问题,幸运的是找到了另一种更好的方法: ViewPropertyAnimator是一种使用简短代码为视图设置动画的好方法. You could write your own method which you can modify and reuse as wanted, something like :您可以编写自己的方法,您可以根据需要对其进行修改和重用,例如:

private void customAnimation(View view, float toX, float toY, int duration, boolean loop) {
    view.animate()
        .translationX(toX)
        .translationY(toY)
        .setDuration(duration)
        .setInterpolator(new LinearInterpolator())
        .setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if(loop) {
                    customAnimation(..., false);
                }
            }
        });

The listener waits for the first animation to finish, so you can call the next animation there.侦听器等待第一个动画完成,因此您可以在那里调用下一个动画。 To play sequentially without looping forever I have added a boolean to the parameters which only executes the next animation if true.为了按顺序播放而不永远循环,我在参数中添加了一个布尔值,如果为真,则只执行下一个动画。

That would also shorten the rest of your code to something like this :这也会将您的其余代码缩短为如下所示:

int prevY = start[1];
int prevX = start[0];
for (int[] square : path) {
    float[] coord = screenFromGrid(square[0], square[1], gridSizeY);
    long speed = (long) (6000.0 / this.displayMetrics.widthPixels);
    speed = (long) (speed * getSquareSize(gridSizeY));

    if (square[0] != prevX) {
        customAnimation(this.sprite1, coord[0], 0, speed, true);
        prevX = square[0];
    } else if (square[1] != prevY) {
        customAnimation(this.sprite1, 0, coord[1], speed, true);
        prevY = square[1];
    }
}

Be sure to check the docs for all available methods, maybe .translationXBy(float byX) is more suited in your case than .translationX(float toX)请务必检查所有可用方法的文档,也许.translationXBy(float byX).translationX(float toX)更适合您的情况

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

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