Inches to pixel on android studios

Started by
12 comments, last by LAURENT* 7 years, 1 month ago

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.

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.

Different devices have different ppi so making things tied to pixels is not the best choice.

Developer with a bit of Kickstarter and business experience.

YouTube Channel: Hostile Viking Studio
Twitter: @Precursors_Dawn

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).

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

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.

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

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.

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.

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.

This topic is closed to new replies.

Advertisement