Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-25386] Android: Alloy/Measurement pxToDP returns wrong result

GitHub Issuen/a
TypeBug
Priorityn/a
StatusOpen
ResolutionUnresolved
Affected Version/sn/a
Fix Version/sn/a
ComponentsAndroid
Labelsn/a
ReporterZakhar Zhuravlev
AssigneeUnknown
Created2017-09-26T08:12:01.000+0000
Updated2018-02-28T19:55:27.000+0000

Description

Ti.Platform.displayCaps.density: xhigh
Ti.Platform.displayCaps.dpi: 320
Ti.Platform.displayCaps.platformHeight: 1200

require("alloy/measurement").pxToDP(Ti.Platform.displayCaps.platformHeight) // 400 wrong
Ti.Platform.displayCaps.platformHeight / (Ti.Platform.displayCaps.dpi / 160) // 600 right

Comments

  1. Hans Knöchel 2017-09-27

    Here is the method implementation:
       
       /**
        * @method pxToDP
        * Converts a screen pixel value to density-independent pixels.
        * @param {Number} val Value in screen pixels.
        * @return {Number} Converted value in density-independent pixels.
        */
       exports.pxToDP = function (val) {
       	if (OS_ANDROID) {
       		return val / dpi * 160;
       	} else if (OS_IOS) {
       		switch (density) {
       			case 'xhigh': return val / 3;
       			case 'high': return val / 2;
       			default: return val;
       		}
       	} else {
       		return val;
       	}
       };
       
    Looks like it's missing the newer densities? A simple PR against [this file](https://github.com/appcelerator/alloy/blob/master/Alloy/builtins/measurement.js) should fix it.

JSON Source