9

我正在使用自定义动画来替换片段,我想在动画开始时禁用一些按钮,然后在动画结束时启用。我怎样才能做到这一点?

4

1 回答 1

28

我的建议是创建一些您的所有Fragments扩展的基类,并在其中定义一些可以被覆盖以处理动画事件的方法。然后,覆盖onCreateAnimation()(假设您正在使用支持库)以在动画回调上发送事件。例如:

protected void onAnimationStarted () {}

protected void onAnimationEnded () {}

protected void onAnimationRepeated () {}

@Override
public Animation onCreateAnimation (int transit, boolean enter, int nextAnim) {
    //Check if the superclass already created the animation
    Animation anim = super.onCreateAnimation(transit, enter, nextAnim);

    //If not, and an animation is defined, load it now
    if (anim == null && nextAnim != 0) {
        anim = AnimationUtils.loadAnimation(getActivity(), nextAnim);
    }

    //If there is an animation for this fragment, add a listener.
    if (anim != null) {
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart (Animation animation) {
                onAnimationStarted();
            }

            @Override
            public void onAnimationEnd (Animation animation) {
                onAnimationEnded();
            }

            @Override
            public void onAnimationRepeat (Animation animation) {
                onAnimationRepeated();
            }
        });
    }

    return anim;
}

然后,对于您的Fragment子类,只需覆盖onAnimationStarted()以禁用按钮并onAnimationEnded()启用按钮。

于 2013-10-27T04:56:02.507 回答