Android画布如何为绘制的圆圈设置动画

时间:2015-07-21 07:49:21

标签: android canvas

嗨,大家好我正在做一个Conect4,我想这样做,因为像素数为200,例如对应的像素或行,圆圈会下降。我之前在XML动画中做过,我知道如何为画布制作动画,但我没有找到一个问题来翻译一个圆圈以使感觉下降。 对不起,如果mi english不好,但我是西班牙Bachillerato学生中的一员。 我在onTocuh和onDraw方法上做了这个:

public class MyView extends View {
    public MyView(Context context) {
        super(context);
    }

    public void CaidaFicha () {
        ValueAnimator caidaFicha = ObjectAnimator.ofInt(this,"caida",200,1000);
        caidaFicha.setDuration(10000);
        caidaFicha.setEvaluator(new IntEvaluator());
        caidaFicha.setRepeatCount(ValueAnimator.INFINITE);
        caidaFicha.setRepeatMode(ValueAnimator.REVERSE);
        caidaFicha.start();
    }

    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int x = getWidth();
        int y = getHeight();
        anchoX = x;
        anchoY = y;
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLUE);
        canvas.drawPaint(paint);
        /*Texto*/
        paint.setColor(Color.BLACK);
        paint.setTextSize(80);
        canvas.drawText("CONECTA 4", 70, 130, paint);
        /*Separador*/
        paint.setColor(Color.parseColor("#5C5C5C"));
        canvas.drawRect(0, 200, 600, 210, paint);
        /*Tablero*/
        int radius = 25;
        for (int i = 0; i < Game.NFILAS; i++)
            for (int j = 0; j < Game.NCOLUMNAS; j++){
                if (game.estaVacio(i,j)){
                    color = Color.WHITE;
                    paint.setColor(color);
                    canvas.drawCircle(getPixelFromColumna(j), getPixelFromFila(i), radius, paint);
                } else if (game.estaJugador(i,j)){
                    paint.setColor(coloreado);
                    canvas.drawCircle(getPixelFromColumna(j), getPixelFromFila(i), radius, paint);
                } else {
                    color = Color.RED;
                    paint.setColor(color);
                    canvas.drawCircle(getPixelFromColumna(j),getPixelFromFila(i), radius, paint);
                }
            }
    }

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

        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            pixel_x = (int) event.getX();
            pixel_y = (int) event.getY();
            fila = getFila(pixel_y);
            columna = getColumna(pixel_x);

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

            if (game.sePuedeColocarFicha(fila, columna) != true) {
                Toast.makeText(getApplicationContext(), R.string.nosepuedecolocarficha,
                        Toast.LENGTH_SHORT).show();
                return false;
            }

            if(pixel_y > 200){

                game.ponerJugador(fila, columna);

                if(game.comprobarCuatro(Game.JUGADOR)){
                    Toast.makeText(getApplicationContext(), "Ha ganado " + nombreFinal,
                            Toast.LENGTH_LONG).show();
                    if(game.fin()) {
                        fragmento_dialogo dialogo = new fragmento_dialogo();
                        dialogo.show(getFragmentManager(), "Alert Dialog");
                    }
                }

                game.juegaMaquina();

                if(game.comprobarCuatro(Game.MAQUINA)){
                    Toast.makeText(getApplicationContext(), "Has perdido " + nombreFinal,
                            Toast.LENGTH_LONG).show();
                    if(game.fin()) {
                        fragmento_dialogo dialogo = new fragmento_dialogo();
                        dialogo.show(getFragmentManager(), "Alert Dialog");
                    }
                }
            }
        }

        invalidate();

        return true;
    }

}

0 个答案:

没有答案