如何禁用onTouchEvent

时间:2015-11-21 08:30:01

标签: android ontouchevent

在我的代码中,我让球落得很好,但如果我触摸屏幕2次,动画会出现2次,如果我触摸6次动画会出现6次。

如何在最后一个动画运行时停止下一个动画? 这是我的代码:

public boolean onTouchEvent(final MotionEvent event) {
        final int columna;
        final int fila;

        pixel_x = (int) event.getX();
        columna = getColumna(pixel_x);
        fila = game.generarFila(columna);

        if (game.tableroLleno()) {
            Toast.makeText(getApplicationContext(), R.string.fin_del_juego,
                    Toast.LENGTH_LONG).show();
        }

        if ((event.getAction() == MotionEvent.ACTION_DOWN) && !pulsado) {
            pulsado = true;
            animator.start();
            animator.setIntValues(200, getPixelFromFila(game.generarFila(columna)));
            animator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                }

                @Override
                public void onAnimationRepeat(Animator animation) {
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    animator.removeAllListeners();
                    game.ponerJugador(fila, columna);
                    if (game.comprobarCuatro(Game.JUGADOR)) {
                        if (Singleton.getInstance().getUsuarioIngresado() != null) {
                            usuarioActual.setPartidasGanadas(usuarioActual.getPartidasGanadas() + 1);
                            gestor.guardar(usuarioActual);
                        }
                        Toast.makeText(getApplicationContext(), "Has ganado " + Singleton.getInstance().getJugadorIngresado(),
                                Toast.LENGTH_LONG).show();
                        if (game.fin()) {
                            FragmentoDialogo dialogo = new FragmentoDialogo();
                            dialogo.show(getFragmentManager(), "Alert Dialog2");
                        }
                    } else {
                        columnaMaquina = game.posicion();
                        while (game.columnaLlena(columnaMaquina)) {
                            columnaMaquina = game.generarColumna();
                        }
                        filaMaquina = game.generarFila(columnaMaquina);
                        animator2.setIntValues(200, getPixelFromFila(filaMaquina));
                        animator2.start();
                        animator2.addListener(new Animator.AnimatorListener() {
                            @Override
                            public void onAnimationStart(Animator animation) {
                            }

                            @Override
                            public void onAnimationRepeat(Animator animation) {
                            }

                            @Override
                            public void onAnimationEnd(Animator animation) {
                                animator2.removeAllListeners();
                                game.ponerMaquina(filaMaquina, columnaMaquina);
                                if (game.comprobarCuatro(Game.MAQUINA)) {
                                    if (Singleton.getInstance().getUsuarioIngresado() != null) {
                                        usuarioActual.setPartidasPerdidas(usuarioActual.getPartidasPerdidas() + 1);
                                        gestor.guardar(usuarioActual);
                                    }
                                    Toast.makeText(getApplicationContext(),
                                            "Has perdido " + Singleton.getInstance().getJugadorIngresado(),
                                            Toast.LENGTH_LONG).show();
                                    if (game.fin()) {
                                        FragmentoDialogo dialogo = new FragmentoDialogo();
                                        dialogo.show(getFragmentManager(), "Alert Dialog2");
                                    }
                                }
                                pulsado = false;
                            }

                            @Override
                            public void onAnimationCancel(Animator animation) {
                            }
                        });
                    }
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                }
            });
        } else {
            return false;
        }
        return super.onTouchEvent(event);
    }

}

1 个答案:

答案 0 :(得分:1)

使用标志(shouldAnimate):

public boolean onTouchEvent(final MotionEvent event) {
    if(shouldAnimate){
    shouldAnimate=false;
        ...
        public void onAnimationEnd(Animator animation) { 
              ...
              public void onAnimationEnd(Animator animation) {
              ...
              shouldAnimate=true;
              }
        ...
    }
}