How do you get pixels per inch when using Application.LETTERBOX? I'd like to position something a certain (real-world) distance from the edge of the screen, regardless of size of display.
local sx,sy=application:getLogicalScaleX(),application:getLogicalScaleY()local ox,oy=application:getLogicalTranslateX(),application:getLogicalTranslateY()local dpi=application:getScreenDensity()local inchX,inchY=dpi/sx,dpi/sy
local screenOriginX,screenOriginY=-ox/sx,-oy/sy
local XdistanceInInches,YdistanceInInches=1,0.5local p=Pixel.new(0xFF0000,1,20,20)
p:setPosition(screenOriginX+inchX*XdistanceInInches,screenOriginY+inchY*YdistanceInInches)
stage:addChild(p)
It will place the 20x20 box top/left corner at 1 inch from left screen border and 0.5 inch from top screen border, whatever your scale mode or device is.
Thanks @hgy29! I tried your code above on an iPod 5th gen, an iPad Mini Retina and a Pixel XL. The dot appears in varying distances on all of them; the iPod and Pixel are roughly in the same place, but the iPad is way off in both directions.
Or maybe reported DPI is wrong for some devices ? you said your iPad Mini retina was 264 dpi, while apple specs say it is 326. And 264/326 is almost 0.8, coincidence ?
Comments in that thread confirm it doesn't work for iPad mini and maybe iphone6 plus, but there doesn't seem to be an universal solution yet, we have to resort on hardcoding them depending on the device type.
Comments
EDIT: yes, your code look fine
Their respective dpi's are:
Pixel XL: 560
iPad Mini Retina: 264
iPod 5th gen: 326
Pixel XL: 1.02x1.02 inches
iPod Touch 5th gen: 1x1 inches
iPad Mini retina: 0.8x0.8 inches
Nexus 5: 1x1 inches
Nexus 6: 1.1x1.1 inches
Maybe the device dpi isn't an exact science?
I found this thread:
http://stackoverflow.com/questions/13178186/how-can-i-detect-the-dpi-on-an-ipad-mini
And:
http://stackoverflow.com/questions/28573639/how-do-ruler-apps-stay-accurate-on-all-devices
Looks like the only way to do it is to hard code the dpi per iOS device. Bummer.
Comments in that thread confirm it doesn't work for iPad mini and maybe iphone6 plus, but there doesn't seem to be an universal solution yet, we have to resort on hardcoding them depending on the device type.
https://github.com/lmirosevic/GBDeviceInfo
Is that what we're using now?
Likes: antix, totebo