1

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

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

4

1 回答 1

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();
于 2019-05-16T13:14:29.423 回答