官术网_书友最值得收藏!

Detecting whether or not a device supports multitouch

When developing projects which target the Android operating system, it is always a good idea to make sure that multitouch is actually supported on the device. In the case of an Android phone, this will probably always be the case, but what about a Google TV or AIR for TV device? Many of these are also Android-based yet most televisions do not have any touch control whatsoever. Never assume the capabilities of any device.

How to do it...

We will need to use internal classes to detect whether or not multitouch is supported:

  1. First, import the following classes into your project:
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.display.Stage;
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.ui.Multitouch;
    
  2. Declare a TextField and TextFormat object to allow visible output upon the device:
    private var traceField:TextField;
    private var traceFormat:TextFormat;
    
  3. We will now set up our TextField, apply a TextFormat, and add it to the DisplayList. Here, we create a method to perform all of these actions for us:
    protected function setupTextField():void {
    traceFormat = new TextFormat();
    traceFormat.bold = true;
    traceFormat.font = "_sans";
    traceFormat.size = 44;
    traceFormat.align = "center";
    traceFormat.color = 0x333333;
    traceField = new TextField();
    traceField.defaultTextFormat = traceFormat;
    traceField.selectable = false;
    traceField.mouseEnabled = false;
    traceField.width = stage.stageWidth;
    traceField.height = stage.stageHeight;
    addChild(traceField);
    }
    
  4. Then, simply invoke Multitouch.supportsGestureEvents and Multitouch.supportsTouchEvents to check each of these capabilities as demonstrated in the following method:
    protected function checkMultitouch():void {
    traceField.appendText(String("Gestures: " + Multitouch.supportsGestureEvents) + "\n");
    traceField.appendText(String("Touch: " + Multitouch.supportsTouchEvents));
    }
    
  5. Each of these properties will return a Boolean value of true or false, indicating device support as shown here:

How it works...

Detecting whether the device supports either touch or gesture events will determine how much freedom you, as a developer, have in refining the user experience. If either of these items returns as false, then it is up to you to provide (if possible) an alternative way for the user to interact with the application. This is normally done through Mouse events:

  • Touch events: Basic interactions such as a single finger tap.
  • Gesture events: More complex interpretations of user interaction such as pinch, zoom, swipe, pan, and so forth.

There's more...

It is important to note that while a specific device may support either gesture events or touch events, when using Flash Platform tools, we must set the Multitouch.inputMode to one or the other specifically.

主站蜘蛛池模板: 左云县| 乐至县| 肥乡县| 万州区| 出国| 璧山县| 元氏县| 宝应县| 玉门市| 鄂托克前旗| 西吉县| 突泉县| 宜都市| 襄垣县| 行唐县| 施甸县| 阿合奇县| 铅山县| 叶城县| 贡觉县| 娄烦县| 清苑县| 新乡市| 紫金县| 贡嘎县| 北宁市| 夏邑县| 瓦房店市| 临漳县| 通许县| 和林格尔县| 南宁市| 宣威市| 无为县| 杭锦旗| 土默特右旗| 广州市| 和政县| 时尚| 乌鲁木齐县| 阳朔县|