3

I have a table-top style game board consisting of 10x10 squares. Each square is a PNG image. On top of these squares I want to place tiles which can be drag and dropped on top of the squares.

What would be my best approach concerning Views?

Is it possible to have two layers where layer one is a grid of ImageView's which represents the board. Would it then be possible to let the tile be an ImageView also which could be "stacked" on top of the ImageView's which represents the board?

Any good design ideas are welcome!

Thanks.

/ Henrik

2 Answers 2

3

Override onTouchEvent in your View:

@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);

    if (!mEnable) {
        return false;
    }

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

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        onTouchActionDown(x, y);
        break;
    case MotionEvent.ACTION_MOVE:
        onTouchActionMove(x, y);
        break;
    case MotionEvent.ACTION_UP:
        onTouchActionUp(x, y);
        break;
    }

    return true;
}

Then in either onTouchActionUp/Down just use the co-ordinates and redraw your image resource, i.e. invalidate() your view.

Override onDraw to draw the dragged graphic:

@Override
protected void onDraw(Canvas canvas)
   drawMyDragGfx();
}
Sign up to request clarification or add additional context in comments.

Comments

2

I did a override on onDraw and onTouchEvent in my View. I.e. I did the drag and drop drawing all by myself.

Worked like a charm :-)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.