У меня большие проблемы с последовательностью анимаций с использованием AnimationSet. Я хочу сделать следующую анимацию:
Увеличьте масштаб от 0 до 100%, затем перейдите от A к B и одновременно к альфа-каналу от 1,0 до 0,5 и от 0,5 до 1,0.
У меня есть несколько проблем с этой последовательностью, но две основные:
Несмотря на то, что я правильно рассчитываю начальное смещение и продолжительность каждой анимации, анимация движения заканчивается, и я все еще на уровне альфа 0,5, поэтому она снова увеличивается до 1,0 один раз в позиции B.
Иногда альфа-анимация не достигает 1,0, похоже, что она останавливается на 0,9 или достигает 1,0, а когда экран перерисовывается и отображается на 0,9.
Насколько мне известно, AnimatorSet оценивает анимации последовательно, но с помощью startOffset он может запускать их параллельно.
Вот кусок моего кода:
public void moveAndDrawPart( ImageView partToMove, Button buttonPressed )
{
// Save current data for listener.
lastPartToMove = partToMove;
lastButtonPressed = buttonPressed;
// Animate part movement.
animatePart(partToMove, buttonPressed);
}
// Animate part movement from original position to the final position.
private void animatePart(ImageView part, Button destination)
{
// Make part visible.
part.setVisibility(ImageView.VISIBLE);
part.bringToFront();
AnimationSet animator = new AnimationSet(false);
animator.setFillAfter(false);
// Make part appears by scaling up.
ScaleAnimation scaleUp = new ScaleAnimation((float)0.0, (float)1.0, (float)0.0, (float)1.0, ScaleAnimation.RELATIVE_TO_SELF, (float)0.5, ScaleAnimation.RELATIVE_TO_SELF, (float)0.5);
scaleUp.setInterpolator(new DecelerateInterpolator());
scaleUp.setFillAfter(true);
scaleUp.setStartOffset(0);
scaleUp.setDuration(500);
// Move from origin to final position.
TranslateAnimation move = new TranslateAnimation(0, destination.getX()-part.getX(), 0, destination.getY()-part.getY());
move.setInterpolator(new DecelerateInterpolator());
move.setFillAfter(true);
move.setStartOffset(scaleUp.getDuration());
move.setDuration(600);
// Make part semi-transparent.
AlphaAnimation alpha = new AlphaAnimation((float)1.0, (float)0.5);
alpha.setInterpolator(new LinearInterpolator());
alpha.setFillAfter(true);
alpha.setRepeatCount(1);
alpha.setRepeatMode(AlphaAnimation.REVERSE);
alpha.setStartOffset(scaleUp.getDuration());
alpha.setDuration(300);
animator.addAnimation(scaleUp);
animator.addAnimation(move);
animator.addAnimation(alpha);
animator.setAnimationListener(this);
part.startAnimation(animator);
}
// Reposition part at final position to redraw it properly.
private void repositionPart(ImageView part, Button destination)
{
// Clear part animation to bring back drawn position to original.
part.clearAnimation();
part.setX(destination.getX());
part.setY(destination.getY());
}
public void onAnimationStart(Animation animation)
{
// This variable is used to workaround a glitch: clearAnimation()
// causes onAnimationEnd() to be called twice.
animationDone = false;
}
public void onAnimationEnd(Animation animation)
{
// This variable is used to workaround a glitch: clearAnimation()
// causes onAnimationEnd() to be called twice.
if(animationDone == false)
{
animationDone = true;
if( (lastPartToMove != null) && (lastButtonPressed != null) )
{
// Reposition the part.
repositionPart(lastPartToMove, lastButtonPressed);
}
else
{
Toast.makeText(myAct, "Major error occured. Last references are not set!", Toast.LENGTH_LONG).show();
}
}
}
Не могли бы вы сказать мне, почему AnimationSet не делает то, что я ожидаю?
Кстати, я экспериментирую с этой проблемой в эмуляторе Honeycomb 3.2.