Search code examples
androidanimationviewanimator

Android Animations are delayed, first animation not running for full length


I'm having some issues with Android Animations with the ViewAnimator.

Here is my code (yes I know I should do the animations themselves in XML but that isn't the problem):

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final ViewAnimator animator = (ViewAnimator)getActivity().findViewById(R.id.animator);
    Button next = (Button)getActivity().findViewById(R.id.buttonNext);
    Button back = (Button)getActivity().findViewById(R.id.buttonBack);
    next.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            animator.setInAnimation(inFromBottomAnimation());
            animator.setOutAnimation(outToTopAnimation());
            animator.showNext();
        }
    });
    back.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            animator.setInAnimation(inFromTopAnimation());
            animator.setOutAnimation(outToBottomAnimation());
            animator.showPrevious();
        }
    });

}

private Animation inFromBottomAnimation() {

    Animation inFromBottom = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
    Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT,  0.0f
    );
    inFromBottom.setDuration(1000);
    inFromBottom.setInterpolator(new AccelerateInterpolator());
    return inFromBottom;
}
private Animation outToTopAnimation() {
    Animation outToTop = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
    Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT, -1.0f
    );
    outToTop.setDuration(1000);
    outToTop.setInterpolator(new AccelerateInterpolator());
    return outToTop;
}

private Animation inFromTopAnimation() {
    Animation inFromTop = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
    Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT,  0.0f
    );
    inFromTop.setDuration(1000);
    inFromTop.setInterpolator(new AccelerateInterpolator());
    return inFromTop;
}
private Animation outToBottomAnimation() {
    Animation outToBottom = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  0.0f,            
    Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT, +1.0f
    );
    outToBottom.setDuration(1000);
    outToBottom.setInterpolator(new AccelerateInterpolator());
    return outToBottom;
}

First of all, the animations are delayed. I have to wait half a second before the animation starts. Second, the first animation that runs in the ViewAnimator either does not run at all (just skips to the next view - after half a second wait though because of the first problem) or it runs too quick to notice.

Is there any workaround/fix to these problems?


Solution

  • Seems to have been an Android problem. The problem is a lot less noticeable on Android 4.1.