链式动画不适用于 AnimatorSet

Chain animations not working with AnimatorSet

我想 "blink" 将视图放大 3 倍(缩小并放大,重复 3 倍)。它有效,但不适用于 AnimatorSet。

这个有效:

ibOffers.animate().scaleX(0.7f).scaleY(0.7f).setDuration(200).withEndAction(new Runnable() {
    @Override
    public void run() {
        ibOffers.animate().scaleX(1.0f).scaleY(1.0f).setDuration(200).withEndAction(new Runnable() {
            @Override
            public void run() {
                ibOffers.animate().scaleX(0.7f).scaleY(0.7f).setDuration(200).withEndAction(new Runnable() {
                    @Override
                    public void run() {
                        ibOffers.animate().scaleX(1.0f).scaleY(1.0f).setDuration(200).withEndAction(new Runnable() {
                            @Override
                            public void run() {
                                ibOffers.animate().scaleX(0.7f).scaleY(0.7f).setDuration(200).withEndAction(new Runnable() {
                                    @Override
                                    public void run() {
                                        ibOffers.animate().scaleX(1.0f).scaleY(1.0f).setDuration(200).withEndAction(new Runnable() {
                                            @Override
                                            public void run() {

                                            }
                                        });
                                    }
                                });
                            }
                        });
                    }
                });
            }
        });
    }
});

但这不是:

final ObjectAnimator scaleDownAnimation =
        ObjectAnimator.ofPropertyValuesHolder(ibOffers,
                PropertyValuesHolder.ofFloat(View.SCALE_X,0.7f),
                PropertyValuesHolder.ofFloat(View.SCALE_Y,0.7f))
        .setDuration(200);

final ObjectAnimator scaleBackAnimation =
        ObjectAnimator.ofPropertyValuesHolder(ibOffers,
                PropertyValuesHolder.ofFloat(View.SCALE_X,1f),
                PropertyValuesHolder.ofFloat(View.SCALE_Y,1f))
                .setDuration(200);

AnimatorSet s = new AnimatorSet();
s.playSequentially(scaleDownAnimation,
        scaleBackAnimation,
        scaleDownAnimation,
        scaleBackAnimation,
        scaleDownAnimation,
        scaleBackAnimation);

我错过了什么?我只是想改进第一个代码块,因为它看起来很丑。

你没有启动它:

s.start();

此外,您不能重复使用 ObjectAnimator。因此,您必须为动画的每个步骤创建三个副本。您可以使用一个小的实用方法来避免重复那么多代码。

您的完整序列将是:

ObjectAnimator getScaleAnimation(View v, float scale, int duration) {
    return ObjectAnimator.ofPropertyValuesHolder(v,
        PropertyValuesHolder.ofFloat(View.SCALE_X, scale),
        PropertyValuesHolder.ofFloat(View.SCALE_Y, scale))
        .setDuration(duration);
}

AnimatorSet s = new AnimatorSet();
s.playSequentially(
    getScaleAnimation(ibOffers, 0.7f, 250),
    getScaleAnimation(ibOffers, 1.2f, 250),
    getScaleAnimation(ibOffers, 0.7f, 250),
    getScaleAnimation(ibOffers, 1.2f, 250),
    getScaleAnimation(ibOffers, 0.7f, 250),
    getScaleAnimation(ibOffers, 1.0f, 250));

s.start();