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.
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?
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.
Related
I have recently completed a simple drag and drop shapes game. It had the user drag a shape (ImageView) to another "empty place holder" ImageView.
I now want to make this a little more advanced, instead of dragging a simple shape, I want to make a puzzle of various non-orthogonal shapes, for example breaking a circle into 5 different pieces. What I'm having a problem with right now is how to design the layout. I do not know how to make a truly "custom" shaped ImageView, as far as I can find from my research it's not possible. So my idea for now is to overlap a number of square ImageViews, each of which will have only a subset of an image and the rest transparent. Thus the final output will look like it's a number of custom shaped ImageViews.
Example:
+ + +
Because only the internal sections are "visible" and the rest of the circle is transparent, when all of these pieces are placed in the same spot on the screen, the final image will look like:
I haven't tried this yet... but I foresee at least one problem. When I go to drag the pieces over to this puzzle, they will all "snap" into place when dragged to the same place. Because in reality all I really have here is a picture of a circle inside a ImageView which has some invisible rectangular boundary around it.
Hopefully this situation is clear. Now my questions:
Is it possible to have truly custom shaped ImageViews instead of my idea of overlapping images?
If what I'm thinking is the best way to handle this puzzle idea, then what property can be changed such that the "drop" action does not happen at the same place for all of these puzzle pieces? If I wanted to "drop" the pizza shaped piece, I'd like it to only snap into place when it go close to the top left of the circle.
Note: I am new to Android programming, and somewhat new to Java/XML as well, it’s very likely I’m overlooking something, so please feel free to suggest other approaches as you see fit.
Not really. Overlapping views is generally the way it's done. You could also use one View and override the drawing action yourself (multiple bitmaps drawn at relative locations within the View), but that would make the drag-drop aspect significantly harder.
If the Views are all the same size, with the visible portions in the correct relative placement in each, they should snap together correctly. This is because the snap is (I believe) based on the position of the upper-left corner of the View. If the pizza-shaped piece's visible portion is correct with regards to that, it should snap in at exactly the right spot.
So you have certain places you want to accept the drops, and I'm assuming you know their coordinates, say (d_x,d_y).
Then why not simply monitor the coordinate of the center (p_x,p_y) of image view of the piece you are dragging, say the "pizza" piece, and when the distance between the the piece and drop point is within an acceptable amount accept the drop.
But if you are asking if there is some way to make non-rectangular image views I don't believe that is possible.
However I don't think it is necessary in your case, because I believe even if you want them to drag the piece precisely into place you can calculate the coordinates where the draggable rectangle needs to go with knowledge of the shape of the piece and the assumption that the rectangle wraps the piece.
I am creating a simple word puzzle where i will need to draw a 16 *16 grid of textviews.
My two options are using a layout or creating one programmatically. I assume the one created programmatically will be very slow while the one created using an xml will be tricky to create. What other options do i have.
If anyone has created one before, can you share your experiences ? The grid will be very much alike a cross word puzzle grid.
Kind Regards,
I would suggest using an XML layout. You can create a reusable style so the XML will be less verbose. It won't be nearly as complicated as the Canvas route because each TextBox will be its own view, with its own handlers. With a Canvas, you would have to manage drawing and detecting change based on coordinates.
1) I am developing an android application for a puzzle that will involve n*n matrix displayed to the user. There will be objects(images) in each of these cells. Whenever the user clicks on the any cell the object of that cell should exchange position with one of the valid nearby cells.
2) When the object exchanges position, the movement should be shown to the user - the image slowly moving from its original place to another place.
If the animation above is very difficult to implement, please leave it. But I would like to know the answer for the question 1). Which layout would be the best for such a puzzle, which will involve constant re positioning of the child elements(cells).
If it's game - SurfaceView is your way. It's good idea, because you can draw from different threads and it's comfortable. If you want to create not simple game, use engine as example AndEngine. So you can use GLSurfaceView - openGL is very fast but it's not simple. And last way use simple view, but my opinion - it can be game like XO, barley-break and other not fast simple game.
upd:
If I create this game as in your case, I will use SurfaceView w/o any layouts. But I think you can do it with help standart view w/o surface. If your question about layout, I think its not matter. For example GridLayout. As minimum it sounds logical.
Good luck!
You would have a greater control if you create a custom view using the View or the SurfaceView.
I am working on a similar n*n matrix game and I am using the AndEngine game engine. It allows me to load images and then place them in an n*n grid.
I'm converting a project that I wrote in AIR a long time ago to native Android. In AIR, positioning views was fairly easy in x,y coordinate systems. With native Android though, I am trying to approach this in a correct way, but I'm unsure how to approach.
My view will consist of two circles in the background, with small objects within those circles that can roll around. There will be another view drawn on top of the circles to make it seem like there is glass over the circle, entrapping the small objects. The small objects are bound to the background circles.
I guess what I'm really asking is canvas drawing the best approach here, or is a view-based layout workable as well?
It sounds like your best option would be to use a SurfaceView. This is basically a hardware accelerated canvas. On of the benefits is that you can overlay standard widgets over top of it if you need to so you can mix and match custom and standard components. Here is a link to a website that walks you through getting a SurfaceView up and running
I'm working on a game that in some ways is similar to Tetris (imagine a 2D array of colored squares that sometimes move around)
I am trying to animate the individual squares so they will smoothly slide down from coordinate to the next. Since I wanted to use Android's built-in tweening feature, the animation has to apply to the whole View (rather than parts of it). This doesn't work well for me because I only want some of the colored squares to slide down, and the rest of them to stay still.
The (theoretical) solution I came up with to resolve this is to make 2 Views, layered directly on top of each other. The top view is for animating squares when they need to move, and the bottom layer is for the static squares. The animation-layer is transparent until I am ready to animate something. I then simply turn on the colored square in the animation-layer, tween it to the new location, and turn it back off when done. In the same time span, the static-layer just turns squares on and off at the right time to make the whole thing look seamless to the end user.
The proposed solution is just a theory, since I haven't been able to make it work correctly yet. Since I have been having trouble, I was wondering if this is even the best way to solve the problem? Perhaps there is a more elegant solution that I am over looking? Anyone know of a better way?
If you just want to animate a single element check out the namespace android.view.animation.Animation. You can also use Drawable shapes and draw them directly. Finally, if you want a simulation then you will have to look into threading. Basically you will create a timer to update the canvas for you based on an interval. There are some other view canvases you can use as well like the GLView canvas.