我们成功使用的另一个/附加选项是一小组函数,它们使用屏幕密度值来计算显示尺寸......这当然只是一个近似值,因为只有四种密度,但我们有发现这很有帮助。
//=============================================================================
// inch
//
// compute approximate number of pixels for the specified physical on-screen
// size based on the density reported by the OS
//=============================================================================
function inch(size)
{
// default to 160 dpi if unavailable
var height = size * 160.0;
try
{
// compute header height based on screen density ... target .25" height
height = size * Ti.Platform.displayCaps.dpi;
}
catch(e) { warn("Error accessing display caps for screen density calculation: " + e); }
return height;
}
因此,如果您希望某些东西在屏幕上高 3/4 英寸......
Ti.UI.createThing({ height: inch(.75)});
...或者如果您想按点大小缩放事物,可以制作一些常量...
const g_12pt = inch(12/72); //0.166667
const g_10pt = inch(10/72); //0.138889
const g_8pt = inch(8/72); //0.111111
const g_6pt = inch(6/72); //0.083333
...
...font:{fontSize: g_10pt, fontWeight:"bold"},...
我们还创建了几个函数来获取屏幕高度和宽度,因此,如果我们想要在平板电脑或其他小型设备上获得更好的布局,我们可以更好地了解我们正在处理的内容。
//=============================================================================
// screenWidth - return screen width in inches
//=============================================================================
function screenWidth()
{
return Titanium.Platform.displayCaps.platformWidth / Titanium.Platform.displayCaps.dpi;
}
//=============================================================================
// screenHeight - return screen height in inches
//=============================================================================
function screenHeight()
{
return Titanium.Platform.displayCaps.platformHeight / Titanium.Platform.displayCaps.dpi;
}
你可以从那里继续……但这确实帮助我们确定了我们如何在不同的密度和平台上布置屏幕。 inch 函数有异常处理只是因为我们在应用程序早期使用它,有时 Ti.Platform 仍然未定义。在我们布置报告时,Ti.Platform 可用,因此屏幕功能没有异常处理。如果您需要提前查询,您可能还需要在这些中进行异常处理。
希望这会有所帮助!