我正在使用自定义动画替换片段,我想在动画开始时禁用一些按钮,然后在动画结束时启用。我该怎么做?

最佳答案

我建议您创建一些基类,所有的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()即可启用按钮。

08-04 07:28