Last active
February 16, 2019 03:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the result of that code: