gpt4 book ai didi

java - 链式动画不适用于 AnimatorSet

转载 作者:行者123 更新时间:2023-11-29 18:31:16 25 4
gpt4 key购买 nike

我想将 View “闪烁”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();

关于java - 链式动画不适用于 AnimatorSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56169294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com