I want to "blink" a view 3x (scale down and back up, repeat 3x). It works, but not with AnimatorSet.
This works:
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() {
}
});
}
});
}
});
}
});
}
});
}
});
But this doesn't:
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);
What am I missing? I just want to improve the first code block as it looks ugly.
You're not starting it:
s.start();
Also, you cannot reuse the ObjectAnimator
. So, you have to create three copies of each step of the animation. You can use a small utility method to not duplicate as much code.
Your complete sequence will be:
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();