## How to work with different screen size

Problems with Canvas, OpenGL, etc...

### How to work with different screen size

Hi all!

Im having doubts on how to work with different screen sizes. Im giving the left top corner to (0,0), and for the right bottom corner i give (screen_width, screen_height). In this way i always know the size of the screen, and keep the aspect ratio of the objects i draw. But what happens when it try it on a smaller/higher screen? well, i keep having the aspect ratio, but have a look to this example:

Screen 1 (landscape):
width = 800
height = 400

I draw an object in (780,380), just in the right bottom corner. See screen 2 example:

Screen 2 (landscape)
width = 600
height = 300

They have same aspect ratio, but if in my code i draw something on (780,380), i some screens it would be visible, and in others it wont. Im thinking in converting coordinates, for example (600*780)/800 = 585, so i have the corresponding coordinates for both screens. But i dont know if this keep the acpect ratio, and i should apply it just not to coordinates, also for object dimensions.

Here is how i setup ortho view:

Code: Select all

public void seUpOrthoProjection(int w, int h)
{
float[] c_matrix = new float[16];
float[] p_matrix = new float[16];

engine.setScreenWidth(w);
engine.setScreenHeight(h);

setUpViewPort(w,h);

Matrix.orthoM(p_matrix, 0, 0, w, h, 0, -1.0f, 1.0f);
Matrix.setLookAtM(c_matrix, 0, 0, 0, 1, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

engine.setProjectionMatrix(p_matrix);
engine.setCameraMatrix(c_matrix);
}

Maybe this is a stupid question, but i dont know what is the way to work with it.

Regards
nsL
Freshman

Posts: 3
Joined: Fri Sep 09, 2011 3:57 pm

### Re: How to work with different screen size

Android provides a few methods for working with different screen sizes -- all of which are normally used in conjunction with eachother.

First of all, when specifying control (view) locations, it is best to use units of dp instead of px. dp are device independent pixels. There isn't a 1:1 correspondence between them, so it will take some finagling to find hte reight numbers, however once you do, you can be assured that the location will be the same no matter what size screen you are on.

For actual assets, such as graphics, you will want to create 3 drawable folders: drawable-hdpi, drawable-mdpi, and drawble-ldpi. Each of these folders should have their own set of graphical assets, sized for the screen category in the folder name.

These are the two simplest ways to handle varying screen sizes, though there are a few more, more complex methods.
jonbonazza
Master Developer

Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

### Re: How to work with different screen size

i think we are talking different things. You mean for making normal android apps, im talking about OpenGL aplications.

Just to resume, i make ortho view based on screen size (sometimes 800x400 and others 600x300 for example). When i draw objects in that view, sometimes they are visible in first screen and not in the second, depending on which coordinates i render them.

If i set always the same resolution (regardless of screen size) for example 800x400, which has an aspect ratio of 1.5, and i try it on a 640x480 screen (aspect ratio 1,333), objects would look out of proportion.
nsL
Freshman

Posts: 3
Joined: Fri Sep 09, 2011 3:57 pm