How to make Standard game design/layout through Surfaceview - java

I have used SurfaceView in my game. Right Now, I have created layout considering one device (320 x 480). The game layout looks nice and all functions are working properly. But when I see the same game in another device with different dimension (screen size), the layout does not seem to be proper.
So is there any property, method, formula or helping Tutorial by which I can create game design/layout through canvas which looks same in all screen size devices.

There is one simple hint: don't ever use absolute coordinates on the SurfaceView. Every item's position should be calculated at runtime depending on the actual screen size. This way you'll achieve a layout that will look the same way on every device. Hope this helps.

Related

One image works fine and other one slow code so much

So I am doing small game in android studio - java. And I was using 400x400 .png image as a ship. It worked well.
Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(),R.drawable.playermove),(int)(length),(int)(height),false);
I am using drawBitmap to draw image on screen.
Now I want to use different ship. Only thing that I changed is that instead of this image I use another one that is 150x150 also .png and game becomes so laggy and sloopy.
Length and Height are 1/10 size of the screen. I have 8 ship pictures with similair dimensions and every one make game sloppy.
Any idea why those pictures make everything sloppy and first one doesn't?
You are doing that on the UI thread. You shouldn't!
I'd recommend not to scale Bitmap dynamically, as bitmap scaling is bit expensive, and if you are doing that in onDraw() which would be triggered every ~17ms, doing redundant/expensive work is bad. I'd suggest you to preprocess the image and keep a scaled version of it and just use it to draw every time.
you are not optimizing your images and this is a background work. try to do this in background.
also this post may help you.

How to keep vlcj consistent when layout changes in JavaFx

I have developed one desktop application using swing under which i have used JavaFx components, i have a few camera which i drag to the 2*2 layout view, it's working fine, now as soon as i changed the layout from 2*2 to 4*4, the view gets changed, then later on the dragged camera remain same on the canvas, but the streaming coming from the camera initially stops and then starts, i just want the video stream coming from the camera to remain consistent, without restarting it, what i have did now, is to release the Media player , Created the updated Canvas and Add it into the panel, but i guess it's not a proper solution, can anyone help me out with this issue, thanks well in advance.
Any kind of help is highly appreciable.
You can not remove the media player Canvas from the frame component hierarchy, nor can you hide it.
You must do something else like minimise it's size to 0,0 use a custom layout manager and move it's position to 0,0 or -1,-1 may work.
To emulate hiding, you could use a CardLayout with a video view and a blank view and switch between them.
There's an example in the vlcj test sources that shows one approach: https://github.com/caprica/vlcj/blob/master/src/test/java/uk/co/caprica/vlcj/test/layout/AdaptiveLayoutTest.java

Simple way to put graphic on the screen in android

I've been looking around for a simple way to put graphic on the screen in android for a while now and I'm really confused.
I've a simple game written in java with swing for graphics and that's all I really need as the graphics doesn't really matter in this project.
Now we want to rewrite it so it works on devices with android and honestly I can't find a simple way to just put a image on the screen.
I'd love to avoid using complex game engine because I just don't need it. All I want is a possibility to draw an image (or 50 images) on x,y given by me and refreshing a screen every 100 milliseconds and I thought I'd be the first thing I'd learn in any android tutorial but well... it's not. Of course I know how to draw an Image with .xml but I need something more automatic - for dozens of images changing all the time.
So what is the best to do this? Isn't there really any way to do it with just some android built in function? If not what engine should I use just for the simplest things I mentioned?
A simple but not necessarily very clean way to do this would be to create an xml layout that is just an empty RelativeLayout.
Then you could create your images like this:
ImageView image = new ImageView(this);
image.setLayoutParams(new RelativeLayout.LayoutParams(IMAGE_WIDTH,IMAGE_HEIGHT));
//Other setup code for your image goes here
myRelativeLayout.addView(image);
This should give you an image of the specified width and height that sits at the origin.
Then you could move the image into position like this:
image.setX(IMAGE_X_POS);
image.setY(IMAGE_Y_POS);
When to do the moving is up to you as it will depend on when you know the positions that need to be moved to.

Android - Place a video at an arbitrary point on a playfield

I need help to place a video at an arbitrary point on a playfield.
I am porting a game which takes place on a playfield - background with animated sprites superimposed. It's working fine.
The activity does a
// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Off that, I run a View on which all the action takes place. The playfield adjusts in size and offset, depending on screen size and orientation. All that's working fine, and I scale the sprites and their positions accordingly.
But sometimes, I need to place a borderless video on top of an existing background at various specific points in the playfield, and everything I read in stackoverflow is about the video itself and not how to position it. Can anyone help? Where should I start looking?
Ah... It appears that I wanted AbsoluteLayout, which became deprecated in 2009. There is, however, more than one way to skin a cat. I just have to work a bit harder.

How to change the position of a view?

I have two views called x and y they are both black lines (for example I made the height of the x line is 1dp and width 230dp and as background filled with the color black).
Now i want to move the position of the lines programmatically (for example I want the y line 50dp to the right of the orginal position).
Can someone help me how to do this?
I have tried things such as setpadding but the line doesn't move.
Thanks in advance!
(ps: my minimum sdk is set for 7 so i can't use the newest api's).
Old Answer
Have a look at the Absolute Layout, it allows you to position
child elements using x, y coordinates. It is deprecated but it's the
only way in Android to do real x,y coordinate positioning.
I would ask what the main point behind what you are trying to do is
though? It sounds like you started with a goal, were led down a path
and now are asking how to get to the end of that path, rather than
asking how to do what you need to do.
Edited
For drawing graphs have a look instead at https://stackoverflow.com/questions/2271248/how-to-draw-charts-in-android.
Using a Layout class to draw a chart will only lead to a really slow app, since the layout classes are designed for creating relatively static layouts, not drawing full graphics.
Instead use either the Canvas and draw your drawables yourself or use the graphing packages listed in the SO question I linked above.

Categories

Resources