Hexagonal board for Android - how to do it?

1.2k views Asked by At

Once again I started to experiment with Android things. I'm trying to create a small simple game, which uses a hexagonal board. I know the math behind the hexagonal calculations, but the problem is how to actually implement such board in Android?

At first I thought of using a grid view where every other line is displaced by half step and then use these square grids as my board and just draw a hexagonal shape over the square boxes. However, the regular grid view doesn't seem to allow me to do that.

Displaced square grid

So I thought that maybe I need to create a custom view with either hexagonal grid or a custom view with square displaced grid.

I'm not familiar with Android custom views. I read a tutorial about custom views from Vogella's tutorials but I'm still very unsure, if it's even possible to create a hexagonal grid as a custom view.

So any ideas and help of how to create and implement a hexagonal game board on android?

1

There are 1 answers

2
chiastic-security On

I would certainly go with a custom view for this. When you draw the view, you can have a loop that draws each hexagonal tile in the right place within the view as a whole. This will give you the most flexibility.

I suggest you start by writing a method that determines, for any given point within the whole view, which hexagonal tile it's in. You'll need this for determining which tile was touched, and it will also help with the drawing.

The nice thing about it is that if you keep track of the centre of each tile, then for any given point, the nearest centre point will tell you which tile the point is located in.