动画正在进行时取消设置onClickListener

时间:2016-12-16 16:42:43

标签: android animation

我正在开发一款Android应用,我已成功将一些图片放在Activity上并为其制作动画。但是当我尝试激活动画时,我发现onClickListener未被禁用。我尝试做一些研究并尝试添加动画监听器适配器并覆盖onAnimationStart()/onAnimationEnd()方法,尝试通过设置 null 来禁用onClickLister,除非它没有工作,经过一些调试,我发现似乎没有调用这两种方法。

以下是我的一些代码:

public class MainActivity extends AppCompatActivity {

//Skipped some of the variables declarations

private class AnimListAdapt extends AnimatorListenerAdapter{

    @Override
    public void onAnimationStart(Animator animation) {
        onClickPointer = null;
        Log.i("Animation","Animation starts");
        super.onAnimationStart(animation);
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        Log.i("Animation","Animation ends");
        super.onAnimationEnd(animation);
        onClickPointer = onClickObject;
    }

};

private AnimListAdapt animListAdapt;
private View.OnClickListener onClickPointer;
private View.OnClickListener onClickObject;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Skipped some of the variable assignments
    onClickObject = new View.OnClickListener() {
        public void onClick(View view) {
            check((ImageView) view);
        }
    };

    onClickPointer = onClickObject;
    animListAdapt = new AnimListAdapt();

    img1View = (ImageView) findViewById (R.id.img1View);
    grids[4] = img1View;
    img1View.setOnClickListener(onClickPointer);

    img2View = (ImageView) findViewById (R.id.img2View);
    grids[4] = img2View;
    img2View.setOnClickListener(onClickPointer);
}
public void check (ImageView view) {
    int move;
if ((previous == null) || ((move = move(findGridNum(previous), findGridNum(view))) == NONE)) {
        if (previous == view) {
            view.setImageResource(android.R.color.transparent);
            previous = null;
        } else {
            if (previous !=null) {
                previous.setImageResource(android.R.color.transparent);
            }
            previous = view;
            view.setImageResource(R.drawable.crush_frame_tran);
        }
    } else {
        previous.setImageResource(android.R.color.transparent);
        switch (move) {
            case UP: {
                Log.i("moveCheck", "Up");
                animListAdapt.setUpAdapter(view);
                view.animate()
                        .setListener(animListAdapt);
                go_up(previous);
                go_down(view);
                break;
            }
            case DOWN: {
                Log.i("moveCheck", "Down");
                animListAdapt.setUpAdapter(view);
                view.animate()
                        .setListener(animListAdapt);
                go_down(previous);
                go_up(view);
                break;
            }
            case LEFT: {
                go_left(previous);
                animListAdapt.setUpAdapter(view);
                view.animate()
                        .setListener(animListAdapt);
                go_right(view);
                break;
            }
            case RIGHT: {
                go_right(previous);
                animListAdapt.setUpAdapter(view);
                view.animate()
                        .setListener(animListAdapt);
                go_left(view);
            }
        }
        previous = null;
    }
}
public void go_right (View view) {
    view.startAnimation(to_right);
}
public void go_left (View view) {
    view.startAnimation(to_left);
}
public void go_up (View view) {
    view.startAnimation(to_up);
}
public void go_down (View view) {
    view.startAnimation(to_down);
}
}

正如您所看到的,我尝试使用onAnimationStart()onAnimationEnd()方法调出日志,我发现这些日志没有出现。我不确定是什么让我的代码不起作用。

请帮帮忙?提前谢谢。

1 个答案:

答案 0 :(得分:0)

mToolbar = mPopupContents.findViewById( R.web.toolbar );
TranslateAnimation anim =
    new TranslateAnimation(0, 0, -60, 0);
anim.setDuration(1000);
anim.setAnimationListener(new Animation.AnimationListener() {
        public void onAnimationStart(Animation a) {
            Log.d(LOGTAG, "---- animation start listener called"  );
        }
        public void onAnimationRepeat(Animation a) {}
        public void onAnimationEnd(Animation a) {
            Log.d(LOGTAG, "---- animation end listener called"  );
        }
    });
mToolbar.startAnimation(anim);

看一下startAnimation()这个例子,你需要传递一个动画实例。但是在你的代码中,你只是通过一个视图,那么你的动画在哪里?这就是onAnimationStart无效的原因。

你不需要像这样做,但我认为你确实移动了一些东西,你可以创建那个动画实例,将其传递给startAnimation()