Skip to content

Instantly share code, notes, and snippets.

@PrimaryFeather
Last active February 16, 2019 03:46
Show Gist options
  • Save PrimaryFeather/505fc4f120c06d09d2c56910476ab710 to your computer and use it in GitHub Desktop.
Save PrimaryFeather/505fc4f120c06d09d2c56910476ab710 to your computer and use it in GitHub Desktop.
This code tests the logic used to come up with the stage size and content scale factor on typical devices.
package
{
import flash.display.Sprite;
public class TestDeviceScaleFactors extends Sprite
{
public function TestDeviceScaleFactors()
{
var testSettings:Array = [
{ name: "iPhone 3GS", dpi: 163, resX: 320, resY: 480 },
{ name: "iPhone 4", dpi: 326, resX: 640, resY: 960 },
{ name: "iPhone 5", dpi: 326, resX: 640, resY: 1136 },
{ name: "iPhone 6", dpi: 326, resX: 750, resY: 1334 },
{ name: "iPhone 6+", dpi: 401, resX: 1242, resY: 2208 },
{ name: "Galaxy S1", dpi: 233, resX: 480, resY: 800 },
{ name: "Galaxy S3", dpi: 306, resX: 720, resY: 1280 },
{ name: "Galaxy S5", dpi: 432, resX: 1080, resY: 1920 },
{ name: "Galaxy S6", dpi: 577, resX: 1440, resY: 2560 },
{ name: "Nexus 1", dpi: 252, resX: 480, resY: 800 },
{ name: "Nexus 4", dpi: 318, resX: 768, resY: 1280 },
{ name: "Nexus 5", dpi: 445, resX: 1080, resY: 1920 },
{ name: "Nexus 6", dpi: 493, resX: 1440, resY: 2560 },
{ name: "ZTE A910", dpi: 267, resX: 720, resY: 1280 },
{ name: "Xperia M5", dpi: 441, resX: 1080, resY: 1920 },
{ name: "iPad", dpi: 132, resX: 768, resY: 1024 },
{ name: "iPad Retina", dpi: 264, resX: 1536, resY: 2048 },
{ name: "iPad Mini", dpi: 163, resX: 768, resY: 1024 },
{ name: "iPad Mini Retina", dpi: 326, resX: 1536, resY: 2048 },
{ name: "Nexus 7 (2012)", dpi: 216, resX: 800, resY: 1280 },
{ name: "Nexus 7 (2013)", dpi: 323, resX: 1200, resY: 1920 }
];
for each (var settings:Object in testSettings)
testDensityToScale(settings.name, settings.dpi, settings.resX, settings.resY);
}
private function testDensityToScale(name:String, dpi:int, resolutionX:int, resolutionY:int):void
{
var iPad:Boolean = name.indexOf("iPad") != -1;
var baseDPI:Number = iPad ? 130 : 160;
var exactScale:Number = dpi / baseDPI;
var scale:Number = 1.0;
if (exactScale < 0.875) scale = 0.75;
else if (exactScale < 1.25) scale = 1.0;
else if (exactScale < 1.75) scale = 1.5;
else scale = Math.round(exactScale);
var stageWidth:int = Math.floor(resolutionX / scale);
var stageHeight:int = Math.floor(resolutionY / scale);
var viewPortWidth:int = stageWidth * scale;
var viewPortHeight:int = stageHeight * scale;
trace(name + " => " + resolutionX + "x" + resolutionY + "@" + dpi + "dpi");
trace(" -> scale factor: " + scale);
trace(" -> stage size: " + stageWidth + "x" + stageHeight);
trace(" -> viewPort size: " + viewPortWidth + "x" + viewPortHeight);
}
}
}
@PrimaryFeather
Copy link
Author

Here's the result of that code:

iPhone 3GS => 320x480@163dpi
  -> scale factor: 1
  -> stage size: 320x480
  -> viewPort size: 320x480
iPhone 4 => 640x960@326dpi
  -> scale factor: 2
  -> stage size: 320x480
  -> viewPort size: 640x960
iPhone 5 => 640x1136@326dpi
  -> scale factor: 2
  -> stage size: 320x568
  -> viewPort size: 640x1136
iPhone 6 => 750x1334@326dpi
  -> scale factor: 2
  -> stage size: 375x667
  -> viewPort size: 750x1334
iPhone 6+ => 1242x2208@401dpi
  -> scale factor: 3
  -> stage size: 414x736
  -> viewPort size: 1242x2208
Galaxy S1 => 480x800@233dpi
  -> scale factor: 1.5
  -> stage size: 320x533
  -> viewPort size: 480x799
Galaxy S3 => 720x1280@306dpi
  -> scale factor: 2
  -> stage size: 360x640
  -> viewPort size: 720x1280
Galaxy S5 => 1080x1920@432dpi
  -> scale factor: 3
  -> stage size: 360x640
  -> viewPort size: 1080x1920
Galaxy S6 => 1440x2560@577dpi
  -> scale factor: 4
  -> stage size: 360x640
  -> viewPort size: 1440x2560
Nexus 1 => 480x800@252dpi
  -> scale factor: 1.5
  -> stage size: 320x533
  -> viewPort size: 480x799
Nexus 4 => 768x1280@318dpi
  -> scale factor: 2
  -> stage size: 384x640
  -> viewPort size: 768x1280
Nexus 5 => 1080x1920@445dpi
  -> scale factor: 3
  -> stage size: 360x640
  -> viewPort size: 1080x1920
Nexus 6 => 1440x2560@493dpi
  -> scale factor: 3
  -> stage size: 480x853
  -> viewPort size: 1440x2559
ZTE A910 => 720x1280@267dpi
  -> scale factor: 1.5
  -> stage size: 480x853
  -> viewPort size: 720x1279
Xperia M5 => 1080x1920@441dpi
  -> scale factor: 3
  -> stage size: 360x640
  -> viewPort size: 1080x1920
iPad => 768x1024@132dpi
  -> scale factor: 1
  -> stage size: 768x1024
  -> viewPort size: 768x1024
iPad Retina => 1536x2048@264dpi
  -> scale factor: 2
  -> stage size: 768x1024
  -> viewPort size: 1536x2048
iPad Mini => 768x1024@163dpi
  -> scale factor: 1
  -> stage size: 768x1024
  -> viewPort size: 768x1024
iPad Mini Retina => 1536x2048@326dpi
  -> scale factor: 2
  -> stage size: 768x1024
  -> viewPort size: 1536x2048
Nexus 7 (2012) => 800x1280@216dpi
  -> scale factor: 1.5
  -> stage size: 533x853
  -> viewPort size: 799x1279
Nexus 7 (2013) => 1200x1920@323dpi
  -> scale factor: 2
  -> stage size: 600x960
  -> viewPort size: 1200x1920

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment