Wrong screen size


Last Updated:

  1. wazzzup

    wazzzup New Member This Topic's Starter

    Joined:
    Aug 2, 2010
    Messages:
    3
    Likes Received:
    0
    I have a HTC Desire and I'm developing a game. I noticed that the dimensions I get from the surfaceChanged callback is 320 x 508 in portrait and 533 x 295 in landscape orientation. This seems too small since the physical screen is 480 x 800. The width from surfaceChanged should be 480 and the heigth should be slightly less because of the status bar.

    Drawing a rectangle with the dimensions from the callback fills out the whole screen!

    I have upgraded to Froyo.

    Am I missing something?
     

    Advertisement
  2. DiscGolfer17

    DiscGolfer17 Member

    Joined:
    Jul 29, 2010
    Messages:
    18
    Likes Received:
    1
    Are you using DisplayMetrics to get the screen size? Also when drawing to a canvas you can use the Canvas.getWidth() and Canvas.getHeight() methods which should give you the correct dimensions. Not sure if you have tried that or not but thought I would I ask.
     
  3. wazzzup

    wazzzup New Member This Topic's Starter

    Joined:
    Aug 2, 2010
    Messages:
    3
    Likes Received:
    0
    Now I have tried both. DisplayMetrics gives a size of 320 x 533 which is consistent with 320 x 508 and 533 x 295 which is the drawable area. canvas.getWidth () and Height () gives the same as the surfaceChanged callback.

    Here are all values from DisplayMetrics:

    Code (Text):
    1. DisplayMetrics{density=1.5, width=320, height=533, scaledDensity=1.5, xdpi=254.0, ydpi=254.0}
    2.  
    3. public float   density        The logical density of the display.
    4. public int     densityDpi     The screen density expressed as dots-per-inch.
    5. public int     heightPixels   The absolute height of the display in pixels.
    6. public float   scaledDensity  A scaling factor for fonts displayed on the display.
    7. public int     widthPixels    The absolute width of the display in pixels.
    8. public float   xdpi           The exact physical pixels per inch of the screen in the X dimension.
    9. public float   ydpi           The exact physical pixels per inch of the screen in the Y dimension.
    10.  
    It seems that the "real" height is heightPixels * density, which is a bit weird. "The absolute height of the display in pixels" should be just that, IMHO.

    I also noticed that coordinates for drawing primitives are floats. This means that they should never be rounded off to integers when drawing.
     
  4. DiscGolfer17

    DiscGolfer17 Member

    Joined:
    Jul 29, 2010
    Messages:
    18
    Likes Received:
    1
    hmmmm... yea that would make sense. Now in your activities onCreate() are you drawing to a place holder such as a SurfaceView or FrameLayout? If so after you get a handle to that layout you should be able to call getHeight() and getWidth() from there on that view. If not you should also be able to do something like...

    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();

    ... to get the absolute height and width of the screen in pixels.

    Let me know if that works, I find it interesting that it is returning those dimensions.
     
  5. wazzzup

    wazzzup New Member This Topic's Starter

    Joined:
    Aug 2, 2010
    Messages:
    3
    Likes Received:
    0
    I draw to a SurfaceView.

    Code (Text):
    1. Display display = getWindowManager().getDefaultDisplay();
    2. int width = display.getWidth();
    3. int height = display.getHeight();
    This also gives 533 x 320.

    The drawing I do looks good, so there is really no problem. It's just weird.
     
  6. DiscGolfer17

    DiscGolfer17 Member

    Joined:
    Jul 29, 2010
    Messages:
    18
    Likes Received:
    1
    Ok, yea that is weird haha. If you do figure out why, I would be interested to know the reason. Things like this bug me haha.
     
  7. iBog

    iBog New Member

    Joined:
    Sep 3, 2010
    Messages:
    1
    Likes Received:
    0
    wazzzup, just add line
    <supports-screens android:anyDensity="true"/>
    to your android manifest and enjoy correct sizes ;)
     

Share This Page

Loading...