dpi to cm wrong measure in different skins

30 views
Skip to first unread message

P5music

unread,
Jan 5, 2021, 6:37:01 AM1/5/21
to CodenameOne Discussions
This question is related to

In my code there is the need to have a standard length. The instruction to do so is:

int frameHeight= (int) (Utils.dpi()* 0.393701);

This should be a centimeter. 
(Utils.dpi() is a convenience method to get the dpi from the Display.getInstance().getDeviceDensity() value)

I tested it with two skins:

Android Note5 (dpi=540 xxhdpi -> 212px)

iOS iPad3  (dpi=240 hdpi -> 94px)

The length measure is used in some HTML, displayed in a BrowserComponent.

When running in simulator I get that
the device is different - OK
the font size and the icons size or images size are the same - OK
the calculated pixels value for 1cm has different size, it is bigger in the Note5 skin.

Should I use CN.convertToPixels()?

Thanks in advance

Shai Almog

unread,
Jan 5, 2021, 10:19:18 PM1/5/21
to CodenameOne Discussions
Look at the skin file properties file to see the ratio variable used to calculate the density of the display. Maybe there was a mistake in generating this specific theme file.
You can see the skins here: https://github.com/codenameone/codenameone-skins

P5music

unread,
Jan 6, 2021, 7:13:07 AM1/6/21
to CodenameOne Discussions

It seems to be a skin issue for many device skins.
These skins show big buttons when the size is from the material icon set. They also show that the cm length is big.

The cm length is meant to be what the simulator CEF BrowserComponent shows as a fixed value. It is not a real cm I think but this is another issue of Milestone 7.0.  It is the same whatever device is chosen as skin, it is sometime blatantly wrong because it is in the simulator, not the device.

So let's consider just the differences from right and wrong cm measure from dpi value.

(Other buttons are instead sized according the dpi constant (not the dpi value) with hard coded measure from the Android version of my app, for example 32,48,72 and so on, they happen to be re right, so they will not be considered.)

Some skins with big buttons (material icon set) and big cm length:
Note5, Pixel2XL, Pixel, Pixel2, HTCA9, HTCM8,Nexus5X,Nexus6P, S8

skins with small buttons (material icon set) and big cm length
iPhone6PLus, iPhone7XPlus, iPhone8Plus, X

skins that have big button size (material icon set) and right cm length, for example:
MotoE, MotoG, Nexus4, GrandPrime, S3,iPhone5c

skins with small buttons (material icon set) and right cm length
iPhone6S, iPhone7, iPhone8

Shai Almog

unread,
Jan 6, 2021, 10:07:07 PM1/6/21
to CodenameOne Discussions
I would assume the problem is in the CEF browser and not in the skin. Did you check the values in the properties file and verify that they match the stated DPI of the device?

P5music

unread,
Jan 7, 2021, 10:38:30 AM1/7/21
to CodenameOne Discussions
I cannot find the dpi value in that list.

The problem is with devices sporting the Display.DENSITY_HD=60 device density, that corresponds to XXHDPI devices.
I had dpi=540 as the corresponding value (it is the value used in calculations), I do not remember why I put that value. 
Also with the dpi=480 value (from Android development website) the cm length is too big in the CEF BrowserComponent, although smaller.

These are the devices with that device density HD=60 -> 540/480 dpi

Android
pixel pixel2 pixel2xl htconea9 htconem8 huaweyp8 nexus5x nexus6p note5 s5 s7 s8
iPhone
X 5plus 6splus 7splus 8splus

Maybe the dpi value is wrong but also maybe the device density value is wrong.
Regards

Shai Almog

unread,
Jan 8, 2021, 3:12:14 AM1/8/21
to CodenameOne Discussions

P5music

unread,
Jan 8, 2021, 4:06:55 AM1/8/21
to CodenameOne Discussions
I read on the internet some formulas involving the pixelRatio but the value in the documents you refer to seems to be an order of magnitude larger than typical ratios. What is the correct formula? What that pixelRatio really mean here?
Regards

Shai Almog

unread,
Jan 9, 2021, 1:14:41 AM1/9/21
to CodenameOne Discussions
Reply all
Reply to author
Forward
0 new messages