Android - Set Canvas to always be 1920x1080p? - java

Im developing a mobile game in Java on a canvas. But when I use different sized phones, the alignments are wrong and the hard coded offset values to correctly place buildings don't match with the new canvas proportions. Also, the UI elements are wrong size now and dont match the screenheight. Feeling kind of lost on how to best solve this issue.
I know that hard-coding the game to always be 1080x1920p will probably lead to black bars on some phones, something I'd also want to avoid if possible.
Canvas.scale did not do the trick, it only achieved crashes.
Anybody got experience in this? Thanks!

Related

What's the best way to have a colored shadow that wraps around any-shaped layout/view?

I've been struggling with shadows in Android for awhile.
So far, I ended up using https://github.com/L-Briand/ShadowLayout most of the time.
Although it works fine for static views of any shape, it bugs a lot in any animation.
I'm looking for a better or easier way. Any suggestions?
My UIs have lot of irregular shapes (not just rounded rectangles) also it has to be at least 26+ (even if the app is 21+, I think it be ok with a lesser alternative for 26-). So, to sum up, what I'm looking for is a library or way of having a shadow:
Can wrap around any shaped layout
It's not a multilayer drawable
Can be animated
Can be colored
At least 26+
Thank you in advance for any suggestions.
I'll pick the best answer in a week (I think that's fair).

Android - Text positioning

I have a very, very, very simple application that I'm working on (been trying my luck at writing an app in Java instead of Unity) and I have a background image that is fullscreen, this image has a lot of design and scales well with every device, and looks great (tested on every resolution we can think of).
However the text doesn't want to stay where it belongs, and that's not permissible. We have a basic setup, similar to this (however with about 6 hours of design work put into it):
and we need text to be centered in the squares at all times. How can we do this? Do we have to manually position and size it for each resolution? That seems like hell.
With a relative layout, if I understand correctly, the boxes will be parents and the text that goes into the boxes will be the children. You can put your TextViews into the boxes and then use things like android:layout_centerVertical or android:layout_centerHorizontal in order to make sure they are centered. Make sure to use sp for your font size so that it scales correctly with the density of the display.

libgdx Cutting an image

I have been trying to "cut" an image for some time now, I ll explain why and what I tried.
So I wanted to create an hp "bar" except it's not a bar but a heart and so I though it would be easy all I had to do is have two pictures draw them on top of each other and then just cut one to make it appear as in hp was being lost, but I was not able to find a way to cut the image.
Setting the height just resizes the image as you might have guessed
I tried using textureRegion to kind of hack it but it didn't go so well
I found a method called clip begin which also uses scissors but for some reason that just doesn't seem to be working.
I might be using the clip begin wrong but I can't really find any real documentation on it, all I'm doing is:
image.clipBegin(x,y,height,weight);
image.clipEnd();
I almost forgot, I'm using a scene2d Image, might be a better way to go around it but not sure what that would be.
I would appreciate any ideas on how to do this, thank you.
You want to use the OpenGL Scissor support that Libgdx exposes. See the Libgdx Clipping wiki
and the Libgdx ScissorStack documentation.
The API isn't particularly friendly (its designed to support dynamically pushing multiple constraining rectangles, which as far as I've seen, isn't used very often).
The important point to remember with the scissor stack is that it only applies to actual draw commands that get issued. Since most APIs try to batch up draw commands, this means actual drawing might not happen when it looks like it should happen. To ensure clipping is happening you must flush any buffered draws before pushing the scissor (otherwise the wrong thing might get clipped) and you must flush any draw calls before popping the scissor (otherwise things you want clipped might avoid the scissors).
See libgdx ScissorStack not working as expected or libGDX - How to clip or How to draw on just a portion of the screen with SpriteBatch in libgdx? or Making a Group hide Actors outside of its bounds.

Use Pixels to set positions and size in Android?

I know people asked how to support different sizes but they only cared about the dimensions of the images. My question is somehow different. I want to know if using pixels is a good way to scale everything and also to position images on the screen.
Is there a better way?
I am creating a 2D game BTW.
Regards,
Baracat
Another way that you can do is to having many sets of images at many resolutions as possible then load it dynamically depends on the screen resolution. I am using this method for menus in my 2D games.
EDIT:
For positioning please have a look at this thread

How to show only part of map on screen

I'm developer of game Space STG II for Android phones... and I want to add to this game new option. To do this I need to know how to do movable screen. I want to do a battle like in Heroes Might & Magic, but in mobile phone screen is small... I don't know how developers show only a part on screen of all things on map. I want to make a small map in corner which show which part of map is shown on screen.
I can't find it :/ I spend 2 days and I found only 2d tile moving, but I need smooth moves. Please help. I will give activation codes for Space STG II for help.
This question is rather vague, and so in return I can't reply with more than a vague answer. Adding this new feature to your game is very specific to how you wrote your game to begin with. If you used bad coding habits, it's going to be very difficult. From what I gather, you are trying to add a mini-map in the corner like many games have. If your model of the game and view are intertwined, you're going to have issues doing this. If you used a design pattern like Model-View-Controller http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller it would be easier to add another "view" of your model and implement it as the minimap. Sorry for the vague answer, but I don't have much else to work with seeing as you have a specific question with not many details of how your game is written.
If you're writing OpenGL code, its extremely easy- just draw the scene and move the camera to look at whatever location you want. Scrolling means moving the camera and will be smooth. Note that a tile approach works fine with OpenGL
If you're using some other 2D API, it might not be that simple. You could try to look up algorithms yourself for how people implement scrolling in widgets (think- how do people make a listview?). You'll come up with something.
Edit: If you do accept this answer, I don't really want your activation codes. Thanks anyway.

Categories