1

私はtictactoeアンドロイドアプリケーションを作っています。TicTacToeGFX という customview を作成したonTouchEvent後、(考慮した後) touch の座標を作成しようとしましたが、対応する四角形をビットマップの円または十字で塗りつぶします。問題は、onTouchEvent で目的のビットマップでキャンバスを再描画できないことです。だから私の質問は、キャンバスの後にキャンバスを再描画するにはどうすればよいonTouchEventですか?

public class NewGame extends Activity { TicTacToeGFX Game;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Game = new TicTacToeGFX(this);
    setContentView(Game);
}

}

public class TicTacToeGFX extends View {

int x, y;
int rectanglescounter = 0;
public Rect[] rectangles = new Rect[9];
// BitMap
Bitmap cross = BitmapFactory.decodeResource(getResources(),
        R.drawable.cross1);
Bitmap circle = BitmapFactory.decodeResource(getResources(),
        R.drawable.circle1);
Bitmap empty = BitmapFactory.decodeResource(getResources(),
        R.drawable.empty1);


Paint paint = new Paint();
Canvas canvas = new Canvas();

public TicTacToeGFX(Context context) {
    super(context);
    setFocusable(true);
}

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // -------------------------------------------calculate
    // screen------------------------------------------------------
    x = (int) ((getWidth()) / 3);
    y = (int) ((getHeight()) / 3);
    // //------------------------------------------- calculate rectangle
    // positions on
    // screen------------------------------------------------------
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            rectangles[rectanglescounter] = new Rect((x * j), (y * i),
                    ((x * j) + x), ((y * i) + y));
            if (rectanglescounter < 8)
                rectanglescounter++;
            else
                break;
        }
    }
    // ------------------------------------------- draw the canvas with
    // empty Bitmap
    // ------------------------------------------------------
    int counter = 0;
    canvas.drawRGB(0, 0, 0);

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            canvas.drawBitmap(empty, null, rectangles[counter], null);
            counter++;

        }
    }

    // ------------------------------------------- draw
    // LinesS------------------------------------------------------
    paint.setColor(Color.WHITE);
    canvas.drawLine(x, 0, x, 3 * y, paint);// 1h
    canvas.drawLine(2 * x, 0, 2 * x, 3 * y, paint);// 2h
    canvas.drawLine(0, y, 3 * x, y, paint);// 3h
    canvas.drawLine(0, 2 * y, 3 * x, 2 * y, paint);// 4h

}

public boolean onTouchEvent(MotionEvent event) {

    int eventaction = event.getAction();

    x = (int) event.getX();
    y = (int) event.getY();

    switch (eventaction) {

    case MotionEvent.ACTION_DOWN: 
        Toast.makeText(getContext(), "Hello", 1).show();


        canvas.drawRGB(0, 0, 0);
        invalidate();

        break;

    case MotionEvent.ACTION_MOVE: // touch drag with the ball
        // move the balls the same as the finger

        break;

    }
    return true;

}
  }
4

0 に答える 0