среда, февраля 11, 2015

AnimatonDrawble - handle animation end / обработать окончание анимации


AnimationDrawble после запуска постоянно возвращает true в результате вызова isRunning, что особенно удивляет в случае onShot = true.

Один из вариантов отследить окончание анимации - посчитать перед стартом ожидаемую продолжительность и, через соответствующий интервал времени, отправить сообщение, инициирующее обработку окончания анимации.


After animationDrawbale was started, isRunning method always returns true regardless the onShot value.
On way to handle animation's end is to calculate the total animation's duration and to send an event after that duration is expired.

private AnimationDrawable timerAnimation;

...

private void startTimerAnimation() {
int timerAnimationDuration = calcTimerAnimationDuration();
timerAnimation.start();
timerAnimationEndHandler.sendEmptyMessageDelayed(0, timerAnimationDuration);
}
   

private int calcTimerAnimationDuration() {
    int total = 0;
    for (int i = 0; i < timerAnimation.getNumberOfFrames(); i++) {
        total += timerAnimation.getDuration(i);
    }
    return total;
}


private Handler timerAnimationEndHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
onTimerAnimatonFinished();
};