I am trying to write an app for android.
Therefore I've got a SurfaceView class drawing all objects.
Now, when for instance I'm drawing a circle, I have to specify a size.
Is there a way making this size depending on how big the screen resolution and density is?
Becausei when drawing a circle, I want it to be the same size on a lower resolution device as on a high resolution device.
When drawing a circle with 50px radius, it would be bigger on smaller screens than on bigger screens.
Any ideas?
EDIT:
I know when creating some layout i could use density-pixels. But I am not working on layouts, i am drawing on top of a surfaceview. when drawing something like
canvas.drawCircle(x, y, size, paint);
size declares the radius in pixel.
Is there a way to convert pixels into density pixels? Any method or simliar?
Thanks in advance
I think the formula you are looking for (dip -> px) is published here:
What is the correct way to specify dimensions in DIP from Java code?
Related
I'm confused about device independent drawing on Android.
Or rather, I have a canvas and I'm just drawing lines, rectangles and ovals on it.
Is there a way of doing this in a device independent viewport so that it just fits whatever screen its on?
I'm not trying to use bitmaps. Or do different layouts for different screen-sizes. I'm not too worried about the aspect ratio being distorted on very odd screens.
So I was hoping there was some kind of mode I could use where it took the arguments to drawOval etc. as device independent and just scaled them to fit the screen at whatever resolution and pixel density it had.
Is there something like this?
Or do I need to recalculate and scale all the coordinates myself?
I know how to handle screen sizes, but it is quite a different matter when using OpenGL ES. The thing is that normally I would just get the size of the screen in pixels and using the numbers given I would align items to be displayed.
But, as I mentioned before, in OpenGL ES it is quite different. I want to draw a simple grid on the screen, but I want all the squares on all the devices to be the same size. That means that with bigger screens I would have more columns and rows instead of bigger squares. So the real question is how to convert screen size in pixels into OpenGL vertex system.
Actually, I was very stupid when asking this. After a bit of experimenting it turned out that the size of the squares of the grid will stay of the same size, because of the way opengl es vertices work. On bigger devices is the size the same as on smaller ones, because the scale stays the same, only the corner indices change accordingly.
I am very beginner to game programming and Libgdx. I am really confused what camera viewport size should I use. In some articles i found that they used 480x800 which is same as target device size. In some articles I found the use meters as 5x5 meter.
So which method is better and how (if you can give some benefits).
If I use meter unites for camera viewport then which is first mapped, width or height.
If i use 5x5 meter for 480x800 pixels device then visible area of world
height = 5 meter = 480px and
width = 800/480 * 5 = 8.33 meter
or
width = 5 meter = 800px and
height = 480/800 * 5 = 3 meter
Is it correct calculation of visible world size and which is used first or second.
I am confused when they start using meter for size everywhere instead of pixels. like actor size is 1x1 meter even it is only 64x64 px. It is really difficult to estimate position and size for me.
Please link any good article about camera and camera units.
Whatever dimensions you specify, they'll be mapped to the entire screen by default. If the aspect ratios don't match, the displayed graphics will be stretched. So if you set your camera to a 5x5 coordinate system on a non-square screen without changing the drawing area, it'll be heavily distorted. If you render it in a square desktop window, it'll be fine.
The advantage of using smaller coordinate systems is that it's easier to calculate with, and possibly more meaningful in the context of a game - e.g. you can think of them as meters, as you said. It's useful in cases where the content matters more than the exact positions on the screen - like drawing the game world.
Using larger coordinates which match the resolution of some devices can be more useful when you're drawing UI. You can see how large you should make each image, for example, if you target that resolution. (Scaling can cause distortions.)
But ultimately, it's a matter of preference. Personally, I like smaller coordinate systems more, so I recently coded my level select menu in a 20*12 system. (I did run into problems when rendering a BitmapFont though - they were not very well made for scaling like this.) Others might prefer to work with resolution-sized coordinates for gameplay rendering as well. What matters is that you should make sure you're not distorting the graphics too much by badly matching aspect ratios.
try 136 for Width and 204 for Height
I have a Android program which you type in equation and them program display you in "new" layout a graph, its like coordinate system.You have function line, x line, y line... like school basic, you know, easy one.
But if your equation numbers are to hight like: "x*x*40" your graph line is to big to be on display. So here i need yur help.
In android you can move picture up, down, left, right, zoom,... and i what to do same with a graph.I found a tutorails like this one:http://obviam.net/index.php/displaying-graphics-with-android/
,but this contains picture and i dont have picture!I have no picture or what so ever. Program works in Canvas and draw lines with command like this:"g.drawLine(x1, y1, x2, y2, color);" and the and it looks somethink like this in full screen:
http://grockit.com/blog/collegeprep/files/2009/12/14.JPG
So here is problem how to move like picture but its not a picture. In a lot of examples you must have a picture like R.drawable.image, but here are just calculated lines.
I have one idea how to do it, but its probably stupid:
-if you made a graph bigger than your screen (much bigger) and than do a screenshot, save like picture and than move like picture as in example
(if you need more explanation i can do it) sry if my English was bad :(
Thank you
Well, your best bet here is to use OpenGL. Otherwise, not only will you have problems with lines sometimes being to big or to small for a given screen, but also with different screen resolutions (your line might be too big for a 320x480 screen, but it will very well become too small for some of the new 1280x720 screens).
Here's what I would do:
make an opengl surface view with ORTHOGONAL projection
make orthogonal projection's "far side" be of high resolution, with fixed width, like maybe 1600
when surface is initialized, the opengl viewport is initted to screen's width and height
also the surface's far side's height will be set to keep the proportions with those of the screen.
you can then use Canvas and its drawxxx() methods to create a bitmap with your graph and text and whatever else you want to display.
then you use that bitmap to make a texture for a rectangular poligon that you draw in your orthogonal perspective.
now the size of the graph will always scale properlly with the user's screen size (like fit in all the time)
also now you can easily add zoom and scroll options
I've got an image that is 800 by 300, which I know is the width of my test platform's resolution (HTC Desire at 800x480). When I try to draw this image to the screen it scales oddly. It spills over the left hand side of the screen and fills almost all the vertical.
I'm using code like this:
canvas.drawBitmap( screen[1], new Rect(0,0,800,300), new Rect(0,0,800,300), null);
For some reason
width_x = canvas.getWidth();
width_y = canvas.getHeight();
reports my resolution as 533 by 320. Now I assume this is for the expletive-deleted fascinating scaling system Android uses so apps appear the same size on all phones but I want to ignore this. I'm writing a game, so want to handle scaling and positioning myself - for instance using more screen estate if it becomes available. Best Android practice may be suitable for an icon based application, but I would like to draw to absolute pixel positions, and get absolute resolution information for the screen.
Therefore my question is this - is this absolutely impossible? If it is completely contraindicated because Android has a simple and effective system in place to do this then I would be interested to know what it is. Dpi is not relevant to my game design (just like if this was a pc game, it would be irrelevant)
Perhaps my screen actually 533 by 320 unless I specify a resolution somehow? I tried using the scaling values from width and height and the image was the correct size on screen, roughly, but had jagged edges because some sort of scaling had occurred. I therefore did not have access to all the pixels my screen is capable of displaying.
To scale it I used something like
canvas.drawBitmap( screen[1], new Rect(0,0,800,300), new Rect((int)(0.0f),(int)(0.0f),(int)(533.0f),(int)(187.5f)), null); // numbers
Just whacked in for testing - ratio equivalent to reported screen resolution. Looks horrid.
Android is not doing anything to mess with your perceived resolution - you are working with 800x480 pixels on that Desire.
Are you working in a fullscreen, custom View and overriding onDraw? Are you working with a SurfaceView? We need to know these things before we can help you with your problem.
Assuming you are doing the above, you should be able to draw your bitmap to the screen without any scaling using Canvas.drawBitmap(Bitmap bitmap, float left, float top, Paint paint). In your case, that would look something like canvas.drawBitmap( screen[1], 0.0f, 0.0f, null); to put it in the upper left corner of your canvas.
In performance-sensitive apps (like games), you don't want to use the source/destination Rect version of drawBitmap() during your draw loop, since it will do the scaling during every iteration of the loop. Instead, draw a scaled/cropped version of your original bitmap to another member bitmap, and draw that one in the loop, using the x/y offset version of drawBitmap linked above.
If you want to know the amount of screen real estate you're working with (and you definitely should, if you're doing any custom drawing), you'll want to override either onSizeChanged() or surfaceChanged(), depending on implementation.
You should really check out the API demos, there are some great examples of how to do exactly what you're trying to do in there.
Good luck!
I fixed one of my problems - in the manifest file the OS I was targeting was set up incorrectly - switching it to 4 (i.e. 1.6) seemed to fix the values I was getting for height and width, at least for the HTC. Emulator is more problematic, but at least its a major step in the right direction. For your info, I'm working in full screen, landscape mode (fixed), with overridden functions for pretty much everything. (Including onDraw, surfaceChanged, and so forth)
If I can get the absolute width and height I can write my own code for loading the correct assets and using the correct scaling for screen positioning - DPI isn't an issue so hopefully that won't stray too far from suggested guidelines.