2

I use applicationDPI in a Flex mobile card game:

<?xml version="1.0" encoding="utf-8"?>
<s:ViewNavigatorApplication 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark"
    firstView="views.Menu"
    applicationDPI="160"
    initialize="init()">

    <fx:Style source="Preferans.css" />

    <fx:Script>
        <![CDATA[
            import spark.core.ContentCache;
            public static const AVATAR_CACHE:ContentCache = new ContentCache();

            public static var SCALE:Number;

            public function init():void {
                SCALE = runtimeDPI / applicationDPI;
            }   
        ]]> 
    </fx:Script>
</s:ViewNavigatorApplication>

And provide assets in 3 different resolutions based on it:

<fx:Declarations>
    <s:MultiDPIBitmapSource id="BACK"
        source160dpi="@Embed('assets/icons/low-res/back.png')"
        source240dpi="@Embed('assets/icons/mid-res/back.png')"
        source320dpi="@Embed('assets/icons/high-res/back.png')"/>
</fx:Declarations>

And still the result doesn't look good, when I for example select an iPad emulator in the Flash Builder 4.6:

enter image description here

While selecting Google Nexus One produces better result:

enter image description here

What to do here, what to use for detection of a phone vs tablet device?

Checking screen resolution wouldn't help here - see the above iPad example (low resolution, but big screen).

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
  • Haven't used Flex for mobile, but I'm pressuming the [Capabilities](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/Capabilities.html) class can provide some handy info like `os,screenResolutionX,screenResolutionY,screenDPI` – George Profenza Nov 02 '12 at 20:37
  • Thanks, but the question is **what** to look for - for example, when comparing iPad vs. Google Nexus One? (please see the screenshots above) – Alexander Farber Nov 02 '12 at 20:44
  • Setting ApplicationDPI manually causes several odd issues; I strongly recommend not setting it. Also, I personally feel that DPI related code can only be tested on devices; the emulator will return the screen's DPI [Usually 72; I think]. I recommend sizing and positioning your elements--cards in this case--based on the space available to you. – JeffryHouser Nov 02 '12 at 20:48

2 Answers2

6
  public static function get isTablet():Boolean  
    { 

       var deviceWidth:Number = Capabilities.screenResolutionX / Capabilities.screenDPI;

       var deviceHeight:Number = Capabilities.screenResolutionY / Capabilities.screenDPI;

       var diagonalInches:Number = Math.sqrt((deviceWidth * deviceWidth)+ (deviceHeight *    deviceHeight));
      if(diagonalInches>=7)
          return true;
      else
          return false;
    }
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
UKM Unnithan
  • 61
  • 1
  • 1
3

For a project I did for work, I had to evaluate the exact same issue. To do so, I created a new class that was run on app init that would evaluate the device and make some decisions.

Essentially, I did this

var deviceWidth:Number = Capabilities.screenResolutionX / Capabilities.screenDPI;
var deviceHeight:Number = Capabilities.screenResolutionY / Capabilities.screenDPI;

That will give you the device width and height on the majority of devices. The screenDPI property is not always accurate, however, so this will not work 100% of the time. It works often enough, however, that I don't think it is an issue.

From there, I did some research. I found that point where a phone stopped being a phone and started being a tablet. There is no standard approach, so I took the smallest popular tablet I could find (at the time, the Blackberry Playbook or Kindle Fire, can't remember which), and used the dimensions of that screen as the breakpoint between phone and tablet.

if ( deviceWidth >= fireWidth && deviceHeight >= fireHeight ) {
    isTablet = true;
}
else {
    isPhone = true;
}

That's pseudo code, obviously, but you get the idea. I also threw in some checks to distinguish between each platform (iOS, Android, and Desktop) and, if it was iOS, I manually set whether it was a tablet or phone because there is a limited field of devices there.

From there, I had two interfaces. One for phones, one for tablets. In the addedToStage function for my Application class, I used the isPhone and isTablet checks to choose which interface to load.

Probably not how it is supposed to be done, nor is it fool proof. Unfortunately, though, that is the closest we can get to a universal application with device-specific interfaces in Adobe AIR, as far as I am aware.

Josh
  • 8,079
  • 3
  • 24
  • 49
  • +1; this is awesome; any chance you can share your full class? I bet it would be useful to many people. ( Bonus points if you can donate it to Apache Flex). – JeffryHouser Nov 02 '12 at 23:16
  • 1
    Sorry, I can't. It was built for a commercial project. I can try to throw together another based on the same concepts this weekend if I get the chance, though. – Josh Nov 02 '12 at 23:31