Jump to content
  • Advertisement
Sign in to follow this  
LAURENT*

Inches to pixel on android studios

This topic is 600 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to convert from inches to pixel. Does anyone know how to do this in android studios. I'm doing this in a java file not an XML file.

Share this post


Link to post
Share on other sites
Advertisement
I'm not aware of any guarantees for pixel dimensions. There are vague guidelines like small and large and extra large and such, but that doesn't equate to inches at all.

Do you need to be exactly correct? I've seen some ruler apps that need to be calibrated the first time so the scale is correct (the user is supposed to zoom in/out until it matches a real measurement held up to the screen) then will show a properly sized measurement after that.

Share this post


Link to post
Share on other sites

xdpi and ydpi of DisplayMetrics are the best bet: https://developer.android.com/reference/android/util/DisplayMetrics.html#xdpi

Do note that on a handful of old devices, the values are very incorrect. In my code, I sanity check the values against the DisplayMetrics densityDpi and if there's a large disparity then I use densityDpi instead (densityDpi is rounded off, so is less accurate, but is more trustworthy because it was historically tested as part of the Android compliance suite where xdpi and ydpi were not).

Edited by C0lumbo

Share this post


Link to post
Share on other sites

i forgot which functions did that but you need at least api 17 for it to work, because api 16 devices don't have such displays.

 

it may be the code i found on stack overflow using google and clicked first hit

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

// will either be DENSITY_LOW, DENSITY_MEDIUM or DENSITY_HIGH
int dpiClassification = dm.densityDpi;

// these will return the actual dpi horizontally and vertically
float xDpi = dm.xdpi;
float yDpi = dm.ydpi;

pixels per inch

 

xdpi
Added in API level 1
float xdpi

The exact physical pixels per inch of the screen in the X dimension.

ydpi
Added in API level 1
float ydpi

The exact physical pixels per inch of the screen in the Y dimension.

 

 

https://developer.android.com/reference/android/util/DisplayMetrics.html

 

 

i spent 2 mintues on findig this

Edited by WiredCat

Share this post


Link to post
Share on other sites

The xdpi/ydpi numbers are wrong on the vast majority of devices.  Some devices they are spot-on, most devices they are mildly wrong, some devices they are complete garbage.

 

The densitydpi numbers are similar.  Some devices they're valid, most they're in a very rough ballpark but only because most devices only vary by a few inches, and they can be garbage.

 

The display's getRealMetrics and getRealSize are the same situation. Some devices they're valid, but don't count on it.

 

 

 

 

What the API writers created does not match what the vendors actually implemented.    There are no APIs that are guaranteed to give you the physical dimensions.  Sometimes they match, usually they don't.  

Much like in PC monitors, it seems like everything uses 96 pixels per inch, software that looks at the actual DPI values is more likely than not going to get incorrect values. They won't be too far wrong, maybe 72 DPI for some, 120 DPI for another, but that's just because monitors are generally a common size for common resolutions. The few pieces of software that use actual physical measurements will calibrate by asking you to hold a ruler up to your screen and measuring for yourself.

Share this post


Link to post
Share on other sites

so we need to list manufacturers that produce garbage devices and tell world about them.

Edited by WiredCat

Share this post


Link to post
Share on other sites
so we need to list manufacturers that produce garbage devices and tell world about them.

 

Samsung, FoxConn, Lenovo, LG, Huawei, Xiaomi... 

 

The thing is that most devices use a stock generic configuration.  The phone manufacturer doesn't particularly care about those details since they don't affect sales.  Maybe they all report 120 DPI and a few are actually 120, but one has 96, another 114, another 140, another 150, another 250.  The values are wrong, but not enough that most users actually care, and certainly not enough to affect sales. 

 

 

Just think about it from what is exposed in the API:  The API creators made a functions that were to return accurate DPI values. They were ignored by vendors.  They created a second set of functions, and those too were set to garbage values by vendors.  Then they created a third set of functions, this time including the word "real" with getRealMetrics and getRealSize so you know they were really serious about them being accurate, and they were generally ignored as well.

 

 

The numbers can be used if you want, but you could just as easily assume a screen size of roughly six inches or so if the device claims to be a phone, and about ten inches if the device claims to be a tablet. In many cases that would be more accurate than what the API reports.

 

Share this post


Link to post
Share on other sites

I think I will assume.

I'll get a whole list of phones and tablets and get their average dimensions. I'll scale everything with this average in mind. It won't be perfect but good enough is good for me.

Share this post


Link to post
Share on other sites
The numbers can be used if you want, but you could just as easily assume a screen size of roughly six inches or so if the device claims to be a phone, and about ten inches if the device claims to be a tablet. In many cases that would be more accurate than what the API reports.

 

Is it really that bad?

We're using those functions and on all phones and tablets I've tried they return reasonable numbers. A lot more reasonable then doing that rough estimation that is clearly very wrong on very many devices. (There are plenty of 7 inch tablet, and phones of many sizes)

I've got a few user reports that might indicate they are not perfect, but I've not yet encountered anything in the wild with actual garbage values.

I'd love to see some actual statistics on how bad it is.

You've succeded in making me paranoid though, I'll definitely keep a closer lookout for it from now...

 

 

I'll get a whole list of phones and tablets and get their average dimensions. I'll scale everything with this average in mind. It won't be perfect but good enough is good for me

 

If you need perfect dimensions at all times you should have some calibration.

If you just need a rough estimate of DPI or physical screensize, I'd use the DisplayMetric values. You will get something roughly right most of the time. Until frob comes up with some stats, I will continue to assume they are roughly right (a few % error) in at least 90% of the devices, which should be "good enough" and a lot less work intensive.

Edited by Olof Hedman

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!