链动画无法与AnimatorSet一起使用

时间:2019-05-16 13:03:57

标签: java android animatorset

我要“闪烁”视图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);

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

1 个答案:

答案 0 :(得分:2)

您没有启动它:

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();