如何避免 acceleration/deceleration 出现在 Animator 动画中?

How can I avoid acceleration/deceleration on Animator animations?

我使用以下代码创建了一系列(最多 4 个)动画。都是同时几个ImageView的简单翻译。

一个动画"frame"(即所有视图的移动)后,onAnimationEnd调用下一帧。

问题是:每次调用 onAnimationEnd 后都有一个短暂但明显的延迟。看起来翻译有一个自动加速和减速以使其看起来更自然,但在这种情况下这是不需要的。 有没有办法避免这种情况?

提前致谢!

private void animateFrame(final int frame) {

    List<Animator> allAnimations = new ArrayList<>();
    AnimatorSet s = new AnimatorSet();

    for (int p = 0; p < playerImages.size(); p++) {

        Animator animation = ObjectAnimator.ofFloat(playerImages.get(p), "translationX", playerPositions[frame][0][p] * factorX);

        allAnimations.add(animation);
        ObjectAnimator animation2 = ObjectAnimator.ofFloat(playerImages.get(p), "translationY", playerPositions[frame][1][p] * factorY);
        allAnimations.add(animation2);
        Log.d(TAG, "onClick: MOVING PLAYER " + p + " TO: " + playerPositions[frame][0][p] + "/" + playerPositions[frame][1][p]);

        if (playerPositions[frame][0][p] != 0) {
            s.playTogether(allAnimations);

            s.setDuration(durations[frame - 1]);
            s.start();

            s.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animator) {

                }

                @Override
                public void onAnimationEnd(Animator animator) {
                    animateFrame(frame+1);
                }

                @Override
                public void onAnimationCancel(Animator animator) {

                }

                @Override
                public void onAnimationRepeat(Animator animator) {

                }
            });

        }
    }

}

引用自Property Animation Overview

An interpolator define how specific values in an animation are calculated as a function of time. For example, you can specify animations to happen linearly across the whole animation, meaning the animation moves evenly the entire time, or you can specify animations to use non-linear time, for example, using acceleration or deceleration at the beginning or end of the animation.

属性 动画的默认插值器是 AccelerateDecelerateInterpolator. But you can set another Interpolator (any class implementing TimeInterpolator)。根据您的问题,我认为您正在寻找 LinearInterpolator:

animation.setInterpolator(new LinearInterpolator());