Android动画延迟,第一个动画没有全长运行

时间:2012-04-24 19:02:58

标签: android animation viewanimator

我在使用ViewAnimator的Android动画时遇到了一些问题。

这是我的代码(是的,我知道我应该在XML中自己动画,但这不是问题):

@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;
}

首先,动画被推迟了。在动画开始之前我必须等待半秒钟。其次,在ViewAnimator中运行的第一个动画要么根本不运行(只是跳到下一个视图 - 因为第一个问题后半秒等待)或者它运行得太快而无法通知。

是否有针对这些问题的解决方法/修复方法?

1 个答案:

答案 0 :(得分:1)

似乎是一个Android问题。在Android 4.1上,这个问题不太明显。